如何使用Python中的服务帐户向Google Cloud进行身份验证?

如何使用Python中的服务帐户向Google Cloud进行身份验证?

问题描述:

我正在尝试制作一个将google存储json文件上传到BigQuery的项目(只需将现在手动完成的工作自动化).

Im trying to make a project that will upload google storage json file to BigQuery (just automate something that is done manually now).

我想为此使用服务帐户",因为我的脚本将每天运行.

And i'd like to use 'service account' for this as my script is going to be run on daily basis.

阅读完所有内容后,我仍然发现我在使用服务帐户时仍在努力进行身份验证.

After reading everything i can found about using service account im still struggling to authenticate.

我想知道是否有人可以检查并指出我想念的东西?

I wonder if someone could check and point me to what i missed?

这是我到目前为止所做的:

Here is what i've done so far:

  1. 为服务帐户创建json密钥文件
  2. 已安装的客户端库:pip install --upgrade google-cloud-bigquery
  3. 根据以下条件安装了Google Cloud sdk: https://cloud.google.com/sdk/docs/
  4. 使用正确指定的密钥路径运行export GOOGLE_APPLICATION_CREDENTIALS=<path_to_service_account_file>
  1. Created json key file for service account
  2. Installed client libraries: pip install --upgrade google-cloud-bigquery
  3. Installed google cloud sdk according to: https://cloud.google.com/sdk/docs/
  4. Run export GOOGLE_APPLICATION_CREDENTIALS=<path_to_service_account_file> with key path specified correctly

现在我正在尝试运行以下python脚本:

Now im trying to run the following python script:

from google.cloud import bigquery
bigquery_client = bigquery.Client()

我收到此错误:

google.auth.exceptions.DefaultCredentialsError:无法自动确定凭据.请设置GOOGLE_APPLICATION_CREDENTIALS或 显式创建凭据并重新运行该应用程序.欲了解更多 信息,请参阅 https://developers.google.com/accounts/docs/application-default-credentials .

google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or explicitly create credential and re-run the application. For more information, please see https://developers.google.com/accounts/docs/application-default-credentials.

我对python和google cloud API还是很陌生,所以很可能错过了一些东西,

Im quite new to both python and google cloud API so possbily missed something,

想知道是否有人可以指出上述步骤中哪里出了什么地方/出了什么问题,或者指出我要为虚拟人清除有关使用服务帐户使用Bigquery设置和运行简单脚本的说明吗?

Wonder if someone could point out where/what was wrong in my steps above or point me to clear instruction for dummys about setting up and running simple script with Bigquery using service account?

我通常在python脚本本身中设置此变量,例如:

I usually set this variable in the python script itself, something like:

import os
from google.cloud.bigquery.client import Client

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'path_to_json_file'
bq_client = Client()