简体   繁体   English

如何使用Python的Google Cloud Client Library配置gcloud项目

[英]How to use Google Cloud Client Library for Python to configure the gcloud project

Hi I am trying to use the Google Cloud Client Library for Python to convert a shell script using the gcloud CLI to launch a container to python. In searching through the documentation ( https://googlecloudplatform.github.io/gcloud-python/ ), I have been unable to find functionality that would allow me to configure the gcloud project.您好,我正在尝试使用 Python 的 Google 云客户端库来转换 shell 脚本,使用 gcloud CLI 将容器启动到 python。在搜索文档时( https://googlecloudplatform.github.io/gcloud-python/ ) ,我一直无法找到允许我配置 gcloud 项目的功能。 For example, for the command-line gcloud config set project <project-name> , I have not come across equivalent functionality with the Python client.例如,对于命令行gcloud config set project <project-name> ,我没有遇到与 Python 客户端等效的功能。 Is anyone aware of this functionality with the client library?有人知道客户端库的这个功能吗?

Actually I didn't get your question right. 实际上,我没有正确回答您的问题。

It is possible, let me just take the example of gcloud storage. 有可能,让我以gcloud存储为例。 When creating a Client instance you can provide a project name: 创建客户端实例时,您可以提供一个项目名称:

class gcloud.storage.client.Client(project=None, credentials=None, http=None)

http://googlecloudplatform.github.io/gcloud-python/stable/storage-client.html http://googlecloudplatform.github.io/gcloud-python/stable/storage-client.html

Note that gcloud-python library and gcloud cli tool are not related. 请注意, gcloud-python库和gcloud cli工具无关。 It is possible for gcloud-python library (or many other libraries) to use application default credentials set up gcloud cli tool, but any other gcloud settings are not exported. gcloud-python库(或许多其他库)可以使用应用程序默认凭据设置gcloud cli工具,但不会导出任何其他gcloud设置。

If you wish to use those settings you can take advantage of --format gcloud-cli flag: 如果您希望使用这些设置,则可以使用--format gcloud-cli标志:

gcloud config list --format=json

which will return json object of all settings which you can parse and use in your script, for example pass project setting to gcloud-python library. 它将返回您可以在脚本中解析和使用的所有设置的 json对象,例如,将项目设置传递给gcloud-python库。

I am not sure I understood your question but if you want to set Google CLI project property, you can use subprocess for this and verify with google.auth :我不确定我是否理解你的问题,但如果你想设置 Google CLI 项目属性,你可以为此使用subprocess并使用google.auth进行验证:

import subprocess
import google.auth


if __name__ == "__main__":
    project_id = 'my-project'
    subprocess.run(['gcloud', 'config', 'set', 'project', project_id, ])
    credentials, project_id = google.auth.default()
    print(project_id)

暂无
暂无

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

相关问题 将使用 google-api-python-client 库的 Python 脚本部署为 Google Cloud Function - Deploying Python script that use google-api-python-client library as Google Cloud Function 在谷歌云功能中找不到 gcloud 命令 Python - gcloud command not found in google cloud Functions Python Google Cloud Storage Python Client Library 中的 list_blobs function 中的分页是如何工作的 - How does paging work in the list_blobs function in Google Cloud Storage Python Client Library 需要使用Python 2.7版本中的“Google-cloud-ndb”库 - Need to use the "Google-cloud-ndb" library in Python 2.7 version 如何使用 gcloud bash 命令检查 Google Cloud Storage 中是否存在文件? - How to check if file exists in Google Cloud Storage with the gcloud bash command? 如何使用服务帐户凭据初始化 Google Cloud Storage NodeJS 客户端库 - How to init Google Cloud Storage NodeJS client library with service account credentials 如何以我使用 gcloud CLI 进行身份验证的方式从 Java 对 google cloud API 进行身份验证 - How to auth google cloud API from Java in the same way I authenticated with gcloud CLI 是否可以在 google.cloud.storage python 客户端中使用 x-goog-if-generation-match? - Is it possible to use x-goog-if-generation-match in google.cloud.storage python client? 如何将 gcloud 与 Babashka 一起使用 - How to use gcloud with Babashka 如何以编程方式获取谷歌云项目编号? - How to get google cloud project number programmaticaly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM