【问题标题】:How to use Google Cloud Client Library for Python to configure the gcloud project如何使用 Google Cloud Client Library for Python 配置 gcloud 项目
【发布时间】:2016-09-26 03:32:21
【问题描述】:

您好,我正在尝试使用适用于 Python 的 Google Cloud 客户端库来转换 shell 脚本,使用 gcloud CLI 将容器启动为 python。在搜索文档 (https://googlecloudplatform.github.io/gcloud-python/) 时,我一直无法找到可以让我配置 gcloud 项目的功能。例如,对于命令行gcloud config set project <project-name>,我没有遇到与Python 客户端等效的功能。有人知道客户端库的这个功能吗?

【问题讨论】:

    标签: python gcloud google-client gcloud-python google-cloud-python


    【解决方案1】:

    其实我没听懂你的问题。

    有可能,我以gcloud存储为例。创建客户端实例时,您可以提供项目名称:

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

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

    【讨论】:

      【解决方案2】:

      请注意,gcloud-python 库和 gcloud cli tool 不相关。 gcloud-python 库(或许多其他库)可以使用application default credentials 设置 gcloud cli 工具,但不会导出任何其他 gcloud 设置。

      如果您想使用这些设置,您可以利用--format gcloud-cli 标志:

      gcloud config list --format=json
      

      这将返回 all settings 的 json 对象,您可以在脚本中解析和使用它,例如将项目设置传递给 gcloud-python 库。

      【讨论】:

        猜你喜欢
        • 2018-04-19
        • 1970-01-01
        • 2021-12-26
        • 2017-04-07
        • 2018-01-11
        • 1970-01-01
        • 2020-07-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多