如何在 Python 中使用服务帐户对 Google Cloud 进行身份验证? [英] How to Auth to Google Cloud using Service Account in Python?

查看:27
本文介绍了如何在 Python 中使用服务帐户对 Google Cloud 进行身份验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个将谷歌存储 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 --upgradegoogle-cloud-bigquery
  3. 根据:https://cloud.google.com/sdk/docs/安装了 google cloud sdk
  4. 运行 export GOOGLE_APPLICATION_CREDENTIALS= 并正确指定密钥路径
  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 和谷歌云 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()

这篇关于如何在 Python 中使用服务帐户对 Google Cloud 进行身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆