简体   繁体   English

在作为 Google Cloud Function 运行的 Python 脚本中使用 gcloud 命令时出错

[英]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 与您的功能一起使用,您可以通过以下方式实现。

  1. Download the appropriate archive . 下载适当的存档
  2. Extract it to your function's folder将其解压缩到您的函数文件夹
  3. Set path to gcloud command to './google-cloud-sdk/bin/gcloud' in the code.在代码中将 gcloud 命令的路径设置为“./google-cloud-sdk/bin/gcloud”。 This may vary depending on where you extracted SDK in your function folder.这可能会因您在函数文件夹中提取 SDK 的位置而异。
  4. Deploy function with either gcloud command or zip it and deploy from Console.使用 gcloud 命令部署函数或将其压缩并从控制台部署。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Google Cloud Platform 上的 Google Cloud Function 中执行 Python 脚本时出错 - Error on executing Python script in Google Cloud Function on Google Cloud Platform 使用谷歌云函数生成python脚本 - Using google cloud function to spawn a python script 使用 gcloud-python 在 Google Cloud Storage 中设置元数据 - Set metadata in Google Cloud Storage using gcloud-python 始终在Google Cloud上运行Python脚本 - Always Running Python Script on Google Cloud 在谷歌云上运行 python 脚本时出现内存错误 - MemoryError when running python script on google cloud 在 Google Cloud Compute Engine 上运行 python 脚本 - Running a python script on Google Cloud Compute Engine 使用依赖项使用 Google Cloud Functions 部署 Python 函数时出错 - Error deploying Python function using Google Cloud Functions using dependencies 使用gcloud-python连接到google bigtable时出现未经身份验证的错误 - Unauthenticated error using gcloud-python to connect to google bigtable 每当在谷歌云存储上创建指定文件时,我想使用云 function 触发 python 脚本 - I want to trigger a python script using a cloud function whenever a specified file is created on the google cloud storage 谷歌云日志python脚本权限错误 - Google cloud logging python script permission error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM