python - 为什么导出 GOOGLE_APPLICATION_CREDENTIALS 不适用于 `bq`?

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

google docs假设使用服务帐户的 JSON key 的路径导出环境变量 GOOGLE_APPLICATION_CREDENTIALS,gcloud 将使用它。我无法让它工作。

我的命令是:

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

这只会引发一个错误:

You do not currently have an active account selected. Please run:

$ gcloud auth login

to obtain new credentials, or if you have already logged in with a different account:

$ gcloud config set account ACCOUNT

to select an already authenticated account to use.

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 来使用这个文件。我正在处理多个项目,因此无法激活任何一个。

最佳答案

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

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

(似乎命令行工具并没有像 gcloud 在代码中用作库时那样进入整个凭证发现链。)


如果您需要使用多个配置(例如身份验证、项目 ID),您可以设置 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 cred 运行 bq ls:

$ CLOUDSDK_ACTIVE_CONFIG_NAME=proj1 bq ls

对于 sa2.json 信誉:

$ CLOUDSDK_ACTIVE_CONFIG_NAME=proj2 bq ls

关于python - 为什么导出 GOOGLE_APPLICATION_CREDENTIALS 不适用于 `bq`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34905714/

相关文章:

python - sh 到 py 转换

google-app-engine - 取消选中时,Google Cloud Storage 公共(public)链接不会失效

node.js - Google 云存储 API 和 Node.js

google-cloud-platform - 在构建时处理 secret ,以便对部署到 Cloud Run 的容器进行单元和集成测试

google-cloud-platform - 恢复 Google Cloud 默认网络

python - 从对象列表创建动态级别嵌套字典?

python - 如何使用或安装pycocoevalcap?

python - 从字符串中的某个位置开始排序

google-cloud-storage - 如何正确授权对 Google Cloud Storage API 的请求?

python - BigQuery API 返回 "No query found"