我是谷歌云存储的新手,我尝试设置一个每天下载一次 blob 的功能。
目前我正在 Jupyter Notebook 中工作,但最终代码将在 Azure Function 中运行。我正在努力设置将我连接到存储桶的客户端。我有一个服务帐户凭证 JSON,它使我能够连接到 google。
我在本地找到了解决方案:
from google.cloud import storage
client = storage.Client.from_service_account_json('<PATH_TO_SERVICE_ACCOUNT_JSON>')
问题是我没有将 JSON 存储在云中的路径,但我将其存储在密钥保管库中。
我想出了以下解决方案:
from google.cloud import storage
import json
from google.oauth2 import service_account
string_key = get_key_from_key_vault()
service_account_info = json.loads(string_key)
google_credentials = service_account.Credentials.from_service_account_info(
service_account_info
)
scoped_credentials = google_credentials.with_scopes(
['https://www.googleapis.com/auth/cloud-platform.read-only'])
print(type(scoped_credentials))
client = storage.Client(credentials = scoped_credentials)
我不完全确定我是否需要scoped_credentials = ...
部分,但我只有存储桶的读取权限。 (如果我跳过该部分,错误保持不变)
当我寻求这个解决方案时,我收到以下错误:
DefaultCredentialsError: Could not automatically determine credentials. Please set
GOOGLE_APPLICATION_CREDENTIALS or explicitly create credentials and re-run the application. For
more information, please see https://cloud.google.com/docs/authentication/getting-started
我不知道我做错了什么,因为我认为我已经明确设置了凭据。
Best
P
你可以设置环境变量GOOGLE_APPLICATION_CREDENTIALS https://cloud.google.com/docs/authentication/getting-started#setting_the_environment_variable使用 json 文件的路径,并通过启动不带参数的存储客户端来验证您的函数。
client = storage.Client()
*默认情况下,存储客户端使用环境变量上的文件路径GOOGLE_APPLICATION_CREDENTIALS
这是使用 JSON 凭证的最简单方法,并且与大多数 Google Cloud python 库兼容。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)