通过 Google Cloud Function (Python) 从 BigQuery 数据库查询

问题描述 投票:0回答:2

我有一个连接到 Google Sheet 的大型查询数据库,我在其中拥有只读访问权限 我的请求是,我想从表中获取数据,并且此请求在 Big Query 编辑器中运行得很好,但我想创建一个 Google Cloud 函数来拥有 API 并直接从 URL 访问此请求 我已经使用此命令创建了一个服务帐户:

gcloud iam service-accounts create connect-to-bigquery
gcloud projects add-iam-policy-binding test-24s --member="serviceAccount:[email protected]" --role="roles/owner"

我创建了一个 Google 云函数,如下所示: 创建云函数

服务帐户设置

这是我的 main.py 文件代码:

from google.cloud import bigquery
def hello_world(request):
 client = bigquery.Client()
 query = "SELECT order_id, MAX(status) AS last_status FROM `test-24s.Dataset.Order_Status` GROUP BY order_id"
 query_job = client.query(query)
 print("The query data:")
 for row in query_job:
   print("name={}, count ={}".format(row[0], row["last_status"]))
 return f'The query run successfully'

对于requirements.txt 文件:

# Function dependencies, for example:
# package>=version

google-cloud-bigquery

该函数部署成功,但是当我尝试测试它时,出现此错误:

Error: function terminated. Recommended action: inspect logs for termination reason. Additional troubleshooting documentation can be found at https://cloud.google.com/functions/docs/troubleshooting#logging Details:
500 Internal Server Error: The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

在阅读日志文件时我发现了这个错误

403 Access Denied: BigQuery BigQuery: Permission denied while getting Drive credentials.

请帮我解决这个问题,我已经尝试了在网上找到的所有解决方案,但没有成功

python google-cloud-platform google-bigquery
2个回答
0
投票

基于此:“获取云端硬盘凭据时权限被拒绝” - 我想说您的服务帐户的 IAM 权限不是“暂时的”=> 虽然该服务帐户可能具有对 BigQuery 的相关访问权限,但它无权访问下划线的内容电子表格保存在云端硬盘上...

我会尝试 - 要么

  1. 扩展服务帐户凭据的范围(如果可能,但这可能不是很简单)。这是 Zaar Hai 的一篇文章,其中包含一些详细信息 - Google Auth — Dispelling the Magic 以及来自 Guillaume 的评论 - “是的,我的经历不一样”;

或者(最好从我的角度来看)

  1. 将基于原始电子表格的表复制(可能定期更新)作为本机 BigQuery 表,并在云函数中使用后者。这种方法的副作用 - 显着提高性能(并节省成本)。

0
投票

这个问题你解决了吗?我也有同样的问题?

© www.soinside.com 2019 - 2024. All rights reserved.