【问题标题】:How to get gsutil to use gcloud credentials in a docker container如何让 gsutil 在 docker 容器中使用 gcloud 凭据
【发布时间】:2018-11-22 00:11:09
【问题描述】:

为什么 gsutil 在 Cloud Shell 上的 docker 容器中运行时不使用 Gcloud 凭据?

根据 [1],gsutil 应在可用时使用 gcloud 凭据:

一旦通过 gcloud auth 配置了凭据,无论用户是否有任何 boto 配置文件(除非在 BOTO_CONFIG 环境变量中指定了不同的路径,这些文件都位于 ~/.boto),都将使用这些凭据。但是,如果需要一种未存储在 gcloud 凭据存储中的非 GCS 凭据(例如,S3 帐户的 HMAC 凭据),gsutil 仍会在 boto 配置文件中查找凭据。

这在 gcloud 安装中似乎可以正常工作,但在 docker 映像中却不行。我在 Cloud Shell 中使用的流程是:

docker run -ti --name gcloud-config google/cloud-sdk gcloud auth login
docker run --rm -ti --volumes-from gcloud-config google/cloud-sdk gcloud compute instances list --project my_project
... (works ok)

docker run --rm -ti --volumes-from gcloud-config google/cloud-sdk gsutil ls gs://bucket/

ServiceException: 401 Anonymous caller does not have storage.objects.list access to bucket.

[1]https://cloud.google.com/storage/docs/gsutil/addlhelp/CredentialTypesSupportingVariousUseCases

【问题讨论】:

    标签: docker google-cloud-platform gcloud gsutil google-cloud-shell


    【解决方案1】:

    您需要使用您的凭据安装卷:

    docker run -v ~/.config/gcloud:/root/.config/gcloud your_docker_image
    

    【讨论】:

    • 凭据已通过“--volumes-from”选项提供。
    • 我的错误是它在我的本地机器上工作,但在 Cloud Shell 上却不行
    • 运行docker run --rm -ti --volumes-from gcloud-config google/cloud-sdk gsutil version -l时没有配置路径
    【解决方案2】:

    以下步骤为我解决了这个问题:

    1. 在 boto 配置文件的 [Credentials] 部分中设置 gs_service_key_file(参见 here)
    2. 使用gcloud auth activate-service-account 激活您的服务帐户
    3. 在gcloud config 中设置您的默认项目

    Dockerfile 被截断:

    ENV GOOGLE_APPLICATION_CREDENTIALS=/.gcp/your_service_account_key.json
    ENV GOOGLE_PROJECT_ID=your-project-id
    
    RUN echo '[Credentials]\ngs_service_key_file = /.gcp/your_service_account_key.json' \
        > /etc/boto.cfg
    RUN mkdir /.gcp
    COPY your_service_account_key.json $GOOGLE_APPLICATION_CREDENTIALS
    
    
    RUN gcloud auth activate-service-account --key-file=$GOOGLE_APPLICATION_CREDENTIALS --project $GOOGLE_PROJECT_ID
    RUN gcloud config set project $GOOGLE_PROJECT_ID
    

    【讨论】:

      【解决方案3】:

      我发现@Alexandre 的回答基本上对我有用,除了一个问题:我的凭据适用于 bq,但不是适用于 gsutil(OP 问题的主题),它返回

      ServiceException: 401 Anonymous caller does not have storage.objects.list access to bucket
      

      相同的凭证怎么可能只适用于一个而不适用于另一个!?

      最终我找到了它:~/.config/configurations/config_default 看起来像这样:

      [core]
      account = xxx@xxxxxxx.xxx
      project = xxxxxxxx
      pass_credentials_to_gsutil = false
      

      为什么?!为什么没有记录?

      无论如何...将标志更改为true,您就完成了排序。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-03-23
        • 2021-07-27
        • 2021-03-19
        • 2018-08-26
        • 2017-11-24
        • 2020-11-12
        • 2020-04-23
        • 2016-09-29
        相关资源
        最近更新 更多