是否可以通过JSON密钥文件本身而非文件路径进行Google Cloud Python身份验证? [英] Is there a way for Google Cloud Python Authentication via the JSON keyfile itself and not the file path?
问题描述
因此对于Ruby,您可以将JSON内容设置为 GOOGLE_CLOUD_KEYFILE_JSON
的环境变量.我正在尝试查找Python文档,但是我只能看到的是加载文件路径身份验证的文档.
So for Ruby, you can set the JSON content into the environmental variable of GOOGLE_CLOUD_KEYFILE_JSON
. I am trying to find the documentation for Python, but all I can see is documentation loading the authentication for file path.
推荐答案
我找到了一种方法,但是我想知道是否有更好的方法.使用 google-auth
库,我可以执行以下操作:
I found a way to do it, but I want to know if there is a better way. Using the google-auth
library, I can do something like this:
import json
from google.cloud import bigquery
from google.oauth2 import service_account
"""
Securely get content of JSON file from protected memory because having
the credential file plainly on the filesystem is unsafe
"""
service_account_info = json.loads(json_file_content_string)
credentials = service_account.Credentials.from_service_account_info(service_account_info)
client = bigquery.Client(project='secret-proj', credentials=credentials)
有关此库的更多信息,请参见: https://google-auth.readthedocs.io/zh-CN/latest/reference/google.oauth2.service_account.html
More about the library here: https://google-auth.readthedocs.io/en/latest/reference/google.oauth2.service_account.html
这篇关于是否可以通过JSON密钥文件本身而非文件路径进行Google Cloud Python身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!