这是我的云函数代码(使用pyhton 3.9),
import os
import subprocess
def is_gcloud_accessible():
command = ["which", "gcloud"]
try:
result = subprocess.run(command, capture_output=True, text=True, check=True)
print(result)
return result.stdout.strip()
except subprocess.CalledProcessError:
return False
def trigger_dataflow_flex_template(req):
project_id = "dataflow-396318"
location = "us-central1"
template_gcs_path = "gs://abc/templates/finalflextemplate.json"
if not is_gcloud_accessible():
print("The gcloud command is not accessible.")
return "The gcloud command is not accessible."
command = [
"gcloud", # Use "gcloud" instead of the full path
"dataflow",
"flex-template",
"run",
"my-job-name",
"--template-file-gcs-location",
template_gcs_path,
"--project",
project_id,
"--region",
location,
]
try:
print(command)
result = subprocess.run(command, capture_output=True, text=True, check=True)
return f"Command output: {result.stdout}"
except subprocess.CalledProcessError as e:
return f"Command failed with error: {e.stderr}"
需求.txt
gcloud
在云 shell 中工作正常,但是当我在云函数中运行相同的东西时,找不到“gcloud”,或者如果我给出的 bin 路径不可访问等
我已经为此苦苦挣扎了三天。请推荐我并成为救世主
安装 gcloud 时未附带 Cloud Functions 系统映像。
相反,您可以使用 Cloud Functions 以外的其他工具,让您能够创建所需的系统映像(使用 docker)以及所需的所有可执行文件。 Cloud Run 和 Compute Engine 可以做到这一点。
还要考虑是否有可以直接调用的 API,而不是通过 gcloud 访问它们。如果您使用 Dataflow,请查看 Dataflow API 文档。