【问题标题】:Why does exporting GOOGLE_APPLICATION_CREDENTIALS not work with `bq`?为什么导出 GOOGLE_APPLICATION_CREDENTIALS 不适用于“bq”?
【发布时间】:2016-04-26 15:12:29
【问题描述】:

google docs 表示导出环境变量 GOOGLE_APPLICATION_CREDENTIALS 以及服务帐户 JSON 密钥的路径,gcloud 将使用它。我无法让它工作。

我的命令是:

GOOGLE_APPLICATION_CREDENTIALS=/home/ubuntu/.config/google-creds.json bq ls

这只是带来一个错误:

您目前没有选择有效的帐户。请运行:

$ gcloud 身份验证登录

获取新凭据,或者如果您已经使用 不同的帐户:

$ gcloud config set account ACCOUNT

选择一个已经过身份验证的帐户来使用。

gcloud -v 给出:

Google Cloud SDK 92.0.0

bq 2.0.18
bq-nix 2.0.18
core 2016.01.12
core-nix 2015.11.24
gcloud
gsutil 4.16
gsutil-nix 4.15

我做错了什么,我该如何解决?我需要gsutilbq 才能使用这个文件。我正在处理多个项目,因此无法特别激活任何一个。

【问题讨论】:

    标签: python google-cloud-storage google-cloud-platform gcloud google-cloud-sdk


    【解决方案1】:

    你试过gcloud auth activate-service-account吗?:

    gcloud auth activate-service-account --key-file google-creds.json
    

    (似乎命令行工具不会像 gcloud 在代码中用作库时那样沿着整个凭证发现链进行。)


    如果你有几个配置(如auth、project ids)需要用到,可以设置configurations。例如,如果您有两个想要使用的服务帐户凭据 - 称它们为 sa1.jsonsa2.json - 您可以执行以下操作:

    $ gcloud config configurations create proj1
    $ gcloud config configurations activate proj1
    $ gcloud auth activate-service-account --key-file sa1.json
    
    $ gcloud config configurations create proj2
    $ gcloud config configurations activate proj2
    $ gcloud auth activate-service-account --key-file sa2.json
    

    现在,您可以使用gcloud config configurations activate 命令来设置您想要使用的配置,或者设置CLOUDSDK_ACTIVE_CONFIG_NAME 环境变量来指定您想要在哪个配置下运行命令。即使用sa1.json 凭据运行bq ls

    $ CLOUDSDK_ACTIVE_CONFIG_NAME=proj1 bq ls
    

    对于sa2.json 信用:

    $ CLOUDSDK_ACTIVE_CONFIG_NAME=proj2 bq ls
    

    【讨论】:

    • 是的,但请参阅我的其他问题的背景:stackoverflow.com/questions/34883810/… 我认为这将是一个更简单的解决方案。
    • 啊 - 你要找的是gcloud config configurations。我会在实际答案中详细说明。
    猜你喜欢
    • 2015-04-02
    • 2017-08-20
    • 1970-01-01
    • 2012-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-24
    相关资源
    最近更新 更多