繁体   English   中英

使用服务帐户凭据连接到 BigQuery?

[英]Connect to BigQuery with Service Account Credentials?

我创建了一个 Google 服务帐户来访问 BigQuery,它提供了一个 .json 凭证文件,如下所示:

{
  "type": "service_account",
  "project_id": "<redacted>",
  "private_key_id": "<redacted>",
  "private_key": "-----BEGIN PRIVATE KEY-----<redacted>\n-----END PRIVATE KEY-----\n",
  "client_email": "<redacted>",
  "client_id": "<redacted>",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "<redacted>"
}

使用机密管理器,我将上面的内容加载到 Python dict bigquery_credentials中,然后尝试连接:

client = bigquery.Client(credentials=bigquery_credentials)

我收到错误:

DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or explicitly create credentials and re-run the application. For more information, please see https://cloud.google.com/docs/authentication/getting-started

显然,我没有以有效方式传递凭据, bigquery.Client无法识别它们。

文档说“将环境变量 GOOGLE_APPLICATION_CREDENTIALS 设置为包含您的服务帐户密钥的 JSON 文件的路径。”

我宁愿避免使用文件系统。 此代码在我可能无法访问本地文件系统的环境中运行。 此外,我正在使用一个秘密管理器:将秘密存储在文件系统上被认为是一种安全风险。

有什么方法可以将服务 account.json 凭据指定给bigquery.Client吗?

我只有这个,您可以使用以下内容:

from google.cloud import bigquery

client = bigquery.Client.from_service_account_json("/path/to/your/file.json")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM