【问题标题】:Google gsutil auth without promptGoogle gsutil auth 没有提示
【发布时间】:2015-10-31 17:21:54
【问题描述】:

我想在 Docker 容器中使用 gsutil。我创建了一个 O2Auth 服务帐户 JSON 文件。

如何设置 gsutil auth 以使用 JSON 配置文件并在不提示的情况下执行命令?

目前我得到这样的东西:

$ gsutil config -e
It looks like you are trying to run "/.../google-cloud-sdk/bin/bootstrapping/gsutil.py config".
The "config" command is no longer needed with the Cloud SDK.
To authenticate, run: gcloud auth login
Really run this command? (y/N) y
This command will create a boto config file at /.../.boto
containing your credentials, based on your responses to the following questions. 
What is the full path to your private key file? 

对于环境提示,我必须使用什么命令/参数/设置?

【问题讨论】:

    标签: docker gcloud gsutil


    【解决方案1】:

    通过执行解决了这个问题:

    gcloud auth activate-service-account --key-file=/opt/gcloud/auth.json
    

    整个示例和完成的容器可以在这里找到:blacklabelops/gcloud

    【讨论】:

    • 有没有办法使用 env vars 而不是 json 文件进行身份验证?
    【解决方案2】:

    如果您只想要 gsutil 并绕过提示,您可以使用期望脚本轻松完成:

    #!/usr/bin/expect
    spawn gsutil config -e
    expect "What is the full path to your private key file?" { send "/path/your.key\r" }
    expect "Would you like gsutil to change the file permissions for you? (y/N)" { send "y\r" }
    expect "What is your project-id?" { send "your-projet-42\r" }
    interact
    

    【讨论】:

      【解决方案3】:

      -o Credentials:gs_service_key_file=<path to JSON file> 做得很好,使用 https://cloud.google.com/storage/docs/gsutil/addlhelp/TopLevelCommandLineOptions 中记录的 boto 配置覆盖参数

      $ gsutil -v
      gsutil version: 4.57
      $ gsutil -o Credentials:gs_service_key_file=key.json ls -al gs://bucket/filename
      79948  2021-05-24T02:12:25Z  gs://bucket/filename#1111111145678393  metageneration=2
      

      【讨论】:

        【解决方案4】:

        上述解决方案对我不起作用。

        为我解决这个问题的方法如下

        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
        

        【讨论】:

          【解决方案5】:

          仅使用 gsutil:

          首先运行此命令手动配置身份验证

          gsutil config -a
          

          这将使用所需的凭据创建 /root/.boto 文件。 将该路径/文件复制到您的 docker 映像中。

          gsutil 现在将使用这些凭据。

          【讨论】:

            猜你喜欢
            • 2022-10-06
            • 1970-01-01
            • 1970-01-01
            • 2022-07-14
            • 2023-02-25
            • 1970-01-01
            • 2016-12-24
            • 2017-09-21
            • 2021-11-25
            相关资源
            最近更新 更多