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

编程入门 行业动态 更新时间:2024-10-27 06:20:28
本文介绍了如何在 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:

  • 为服务帐户创建了 json 密钥文件
  • 安装的客户端库:pip install --upgradegoogle-cloud-bigquery
  • 根据:cloud.google/sdk/docs/安装了 google cloud sdk
  • 运行 export GOOGLE_APPLICATION_CREDENTIALS= 并正确指定密钥路径
  • Created json key file for service account
  • Installed client libraries: pip install --upgrade google-cloud-bigquery
  • Installed google cloud sdk according to: cloud.google/sdk/docs/
  • 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 或显式创建凭据并重新运行应用程序.更多信息,请看developers.google/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 developers.google/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 进行身份验证?

    本文发布于:2023-10-10 12:14:55,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1478643.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:身份验证   帐户   如何在   Python   Google

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!