使用参数调用 Google 云函数
Calling Google Cloud function with arguments
我有一个从云存储中获取 sql 文件的功能。这个函数接受 project_id,bucket_id & sql_file
from google.cloud import storage
def read_sql(request):
request_json = request.get_json(silent=True)
project_id=request_json['project_id']
bucket_id=request_json['bucket_id']
sql_file=request_json['sql_file']
gcs_client = storage.Client(project_id)
bucket = gcs_client.get_bucket(bucket_id)
blob = bucket.get_blob(sql_file)
contents = blob.download_as_string()
return contents.decode('utf-8')
当我用这些参数测试它时它工作正常
{"project_id":"my_project","bucket_id":"my_bucket","sql_file":"daily_load.sql"}
我正在尝试在 Google WorkFlow 中调用此函数,但不知道如何在调用 http.get
中设置参数
main:
params: []
steps:
- init:
assign:
- project: ${sys.get_env("GOOGLE_CLOUD_PROJECT_ID")}
- location: "us-central1"
- name: "workflow_test_return_sql1"
- service_account: "sa@appspot.gserviceaccount.com" # Use App Engine default SA.
- get_function:
call: googleapis.cloudfunctions.v1.projects.locations.functions.get
args:
name: ${"projects/" + project + "/locations/" + location + "/functions/" + name}
result: function
- grant_permission_to_all:
call: googleapis.cloudfunctions.v1.projects.locations.functions.setIamPolicy
args:
resource: ${"projects/" + project + "/locations/" + location + "/functions/" + name}
body:
policy:
bindings:
- members: ["allUsers"]
role: "roles/cloudfunctions.invoker"
- call_function:
call: http.get
args:
url: ${function.httpsTrigger.url}
result: resp
Workflow中编写的代码是yaml
知道如何构建带参数的函数调用 URL 吗?
我尝试了以下两种方法,但都没有用
- call_function:
call: http.post
args:
url: ${function.httpsTrigger.url}
body:
input: {"project_id":"my_project","bucket_id":"my_bucket","sql_file":"daily_load.sql"}
result: resp
和
- call_function:
call: http.get
args:
url: ${function.httpsTrigger.url}?project_id=my_project?bucket_id=my_bucket?sql_file=daily_load.sql
你很接近!!试试看
- call_function:
call: http.get
args:
url: ${function.httpsTrigger.url + "?project_id=" + my_project + "&bucket_id=" + my_bucket + "&sql_file=" + daily_load.sql}
编辑 1
这里是 post 选项和 JSON 正文(注意 YAML 和 JSON 是相似的。这里是如何用 yaml 写你的 JSON)
- call_function:
call: http.post
args:
url: ${function.httpsTrigger.url}
body:
project_id: "my_project"
bucket_id: "my_bucket"
sql_file: "daily_load.sql"
result: resp
我有一个从云存储中获取 sql 文件的功能。这个函数接受 project_id,bucket_id & sql_file
from google.cloud import storage
def read_sql(request):
request_json = request.get_json(silent=True)
project_id=request_json['project_id']
bucket_id=request_json['bucket_id']
sql_file=request_json['sql_file']
gcs_client = storage.Client(project_id)
bucket = gcs_client.get_bucket(bucket_id)
blob = bucket.get_blob(sql_file)
contents = blob.download_as_string()
return contents.decode('utf-8')
当我用这些参数测试它时它工作正常
{"project_id":"my_project","bucket_id":"my_bucket","sql_file":"daily_load.sql"}
我正在尝试在 Google WorkFlow 中调用此函数,但不知道如何在调用 http.get
中设置参数main:
params: []
steps:
- init:
assign:
- project: ${sys.get_env("GOOGLE_CLOUD_PROJECT_ID")}
- location: "us-central1"
- name: "workflow_test_return_sql1"
- service_account: "sa@appspot.gserviceaccount.com" # Use App Engine default SA.
- get_function:
call: googleapis.cloudfunctions.v1.projects.locations.functions.get
args:
name: ${"projects/" + project + "/locations/" + location + "/functions/" + name}
result: function
- grant_permission_to_all:
call: googleapis.cloudfunctions.v1.projects.locations.functions.setIamPolicy
args:
resource: ${"projects/" + project + "/locations/" + location + "/functions/" + name}
body:
policy:
bindings:
- members: ["allUsers"]
role: "roles/cloudfunctions.invoker"
- call_function:
call: http.get
args:
url: ${function.httpsTrigger.url}
result: resp
Workflow中编写的代码是yaml
知道如何构建带参数的函数调用 URL 吗?
我尝试了以下两种方法,但都没有用
- call_function:
call: http.post
args:
url: ${function.httpsTrigger.url}
body:
input: {"project_id":"my_project","bucket_id":"my_bucket","sql_file":"daily_load.sql"}
result: resp
和
- call_function:
call: http.get
args:
url: ${function.httpsTrigger.url}?project_id=my_project?bucket_id=my_bucket?sql_file=daily_load.sql
你很接近!!试试看
- call_function:
call: http.get
args:
url: ${function.httpsTrigger.url + "?project_id=" + my_project + "&bucket_id=" + my_bucket + "&sql_file=" + daily_load.sql}
编辑 1
这里是 post 选项和 JSON 正文(注意 YAML 和 JSON 是相似的。这里是如何用 yaml 写你的 JSON)
- call_function:
call: http.post
args:
url: ${function.httpsTrigger.url}
body:
project_id: "my_project"
bucket_id: "my_bucket"
sql_file: "daily_load.sql"
result: resp