[英]Getting trouble in connecting with Google BigQuery
我一直在 Google App Engine 上用 flask 制作 api,当我在部署后从浏览器向这个应用程序发送请求时,出现 502 错误。 我确定此错误是由“gcloud app logs tail -s test”的 GCP 凭据引起的,但凭据 Json 文件和文件名的路径似乎没问题。 我用谷歌搜索并尝试了在那里找到但无法解决的每篇文章。 我已经完成 export GOOGLE_APPLICATION_CREDENTIALS="/home/user/secret_key/bq.json" 谁能告诉我解决方案? 如果缺少任何信息,请告诉我。 谢谢你。
此外,我的 api function 正在通过 http 请求获取 luid 参数并使用该 luid 运行 SQL,如果 luid 的行在 BigQuery 的 cv_date 列中有数据,它会向客户端返回 True。
【The result of "gcloud app logs tail -s test"】
File "/env/lib/python3.7/site-packages/google/auth/_default.py", line 97, in load_credentials_from_file "File {} was not found.".format(filename) google.auth.exceptions.DefaultCredentialsError: File /home/user/secret_key/bq.json was not found.
【/home/user/api_dev/main.py】
from flask import Flask,request
from google.cloud import bigquery
import os
credentials_json = '/home/user/secret_key/bq.json'
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credentials_json
client = bigquery.Client()
app = Flask(__name__)
@app.route('/')
def get_request():
request_luid = request.args.get('luid') or ''
query = """
SELECT EXISTS(SELECT cv_date FROM `test-266110.conversion_log.conversion_log_202008*` t WHERE request_luid = p.luid)
"""
query_res = client.query(query)
return query_res
if __name__ == "__main__":
app.run()
【Remove the codes for BigQuery except import library and variables】
*This code works well and returns luid you input on url parameter
from flask import Flask, request
from google.cloud import bigquery
import os
credentials_json = '/home/user/secret_key/bq.json'
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credentials_json
app = Flask(__name__)
@app.route('/')
def get_request():
request_luid = request.args.get('luid') or ''
return request_luid
if __name__ == "__main__":
app.run()
我建议通读授权文档。
https://cloud.google.com/docs/authentication/production更详细地讨论了服务帐户交互。 您可能不需要在实时应用程序中传递您的凭据。 您可以在本地运行时简单地设置 GOOGLE_APPLICATION_CREDENTIALS 以使用凭据,但您不需要在生产中设置它。
问题是您指定的路径 ( /home/user/secret_key/bq.json
) 仅对您的开发环境有效,并且根本不包含在您的生产部署中或部署文件的绝对路径应用程序是不同的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.