BigQuery - Scheduled Query Error "PermissionDenied: 403



有一个问题与预定查询,使用python脚本,我得到错误:"google.api_core.exceptions。PermissionDenied: 403调用者在运行下面的脚本时没有权限。

#!/usr/bin/python
import sys
import json
import time
from google.cloud import bigquery_datatransfer
from google.oauth2 import service_account
prj_id = "project-id"
ds_id = "dataset_id"
gcp_info = json.load(open("key-file.json"))
creds = service_account.Credentials.from_service_account_info(gcp_info)
s_creds = creds.with_scopes(
[
'https://www.googleapis.com/auth/cloud-platform',
'https://www.googleapis.com/auth/bigquery',
]
)
s_acc = "service-account@project_id.iam.gserviceaccount.com"
bq_tc = bigquery_datatransfer.DataTransferServiceClient(credentials=s_creds)
dataset = prj_id + '.' + ds_id + '.'
def main():
argc = len(sys.argv) - 1
if argc != 1:
print("Usage: python3 /root/gcp_new-query.py <Temp-Table>")
sys.exit()

t_id = dataset + sys.argv[1] + '-temp'
t2_id = dataset + sys.argv[1] + '-data'
q2 = """
DELETE FROM `{}` WHERE AddressID > 0 AND MsgTS < TIMESTAMP_SUB(CURRENT_TIMESTAMP(), 
INTERVAL 60 MINUTE)
""".format(t_id)
p = bq_tc.common_project_path(prj_id)
tc_cfg2 = bigquery_datatransfer.TransferConfig(
destination_dataset_id=ds_id,
display_name=sys.argv[1]+"-RM-Old-Data",
data_source_id="scheduled_query",
params={
"query": q2,
},
schedule="every hour",
)
tc_cfg2 = bq_tc.create_transfer_config(
bigquery_datatransfer.CreateTransferConfigRequest(
parent=p,
transfer_config=tc_cfg2,
service_account_name=s_acc,
)
)
print("Created scheduled query '{}'".format(tc_cfg2.name))
main()

一旦进入create_transfer_config(),我就会得到错误。我已经浏览了文档,并确保所有正确的权限都被授予了"service-account@project_id",即:

  • BigQuery Data Transfer Service Agent
  • BigQuery管理

我是否缺少权限,或者我的脚本中是否存在不完全正确的内容?如果我没有把每件事都解释清楚,我很抱歉。

编辑:我还确保服务帐户有一个关联的密钥json文件。

——.

最后我找到了解决办法。我只是使用了:

import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/path/to/json.json"

代替:

gcp_info = json.load(open("key-file.json"))
creds = service_account.Credentials.from_service_account_info(gcp_info)
s_creds = creds.with_scopes(
[
'https://www.googleapis.com/auth/cloud-platform',
'https://www.googleapis.com/auth/bigquery',
]
)
s_acc = "service-account@project_id.iam.gserviceaccount.com"

相关内容

  • 没有找到相关文章

最新更新