[英]Error using gcloud command in Python script running as Google Cloud Function
I have below python code running in Google Cloud function.我在 Google Cloud 函数中运行了以下 python 代码。
pname = format(os.environ.get('GCP_PROJECT'))
project_metadata_file = "/tmp/" + pname + "_proj_metadata.json"
bucket_name = "XXXX"
H = open(project_metadata_file, "w")
project_list_command = "gcloud projects describe " + pname + " --format json"
project_output = subprocess.check_output(shlex.split(project_list_command))
project_output_json = json.loads(project_output)
H.write(project_output_json)
H.close()
upload_blob(bucket_name, project_metadata_file, os.path.basename(project_metadata_file))
return pname
Here is the error I am getting from the Cloud function.这是我从 Cloud 函数中得到的错误。 Could you please advise how can I have gcloud as a requirement for this cloud function.
您能否建议我如何将 gcloud 作为此云功能的要求。 Please advise.
请指教。
Error: function terminated. Recommended action: inspect logs for termination reason. Details:
[Errno 2] No such file or directory: 'gcloud': 'gcloud'
Thanks谢谢
You have to expect your function as being deployed into a container that includes, in your case, a Python runtime.您必须期望您的函数被部署到一个容器中,在您的情况下,该容器包括一个 Python 运行时。 You can not assume that your cloud function includes a shell environment with cloud sdk installed.
您不能假设您的云功能包括安装了云 sdk 的 shell 环境。 I would recommend using Method: projects.get API .
我建议使用Method: projects.get API 。 In case you want to use the cloud sdk commands then Google Cloud Run would be one of the solutions.
如果您想使用 cloud sdk 命令,那么 Google Cloud Run 将是解决方案之一。
Enter the project name and you will receive this output:输入项目名称,您将收到以下输出:
{
"projectNumber": "",
"projectId": "",
"lifecycleState": "",
"name": "",
"createTime": "",
"parent": {
"type": "",
"id": ""
}
}
As DazWilkin already mentioned, your code will be much cleaner and robust if you use specialized library.正如 DazWilkin 已经提到的,如果您使用专门的库,您的代码将更加简洁和健壮。
If you really need to use Cloud SDK with your function, you can achieve it with the following.如果您确实需要将 Cloud SDK 与您的功能一起使用,您可以通过以下方式实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.