gpt4 book ai didi

python - 如何从 python 函数登录 Google Cloud Storage?

转载 作者:行者123 更新时间:2023-12-04 11:40:17 27 4
gpt4 key购买 nike

我是谷歌云存储的新手,我尝试设置一个每天下载一次 blob 的功能。
目前我正在我的 Jupyter Notebook 中工作,但最终,代码将在 Azure 函数中运行。我正在努力设置将我连接到存储桶的客户端。我有一个服务帐户凭据 JSON,它使我能够连接到 google。
在本地我找到了一个解决方案:

from google.cloud import storage

client = storage.Client.from_service_account_json('<PATH_TO_SERVICE_ACCOUNT_JSON>')
问题是我没有将 JSON 存储在云中的路径,但我将其存储在 key 保管库中。
我想出了以下解决方案:
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
我不知道我做错了什么,因为我认为我已经明确设置了凭据。
最好的事物

最佳答案

经过更多测试后,我发现我错过添加 project = None .
如果您添加它并使用以下命令来创建它可以工作的客户端:

client = storage.Client(project = None, credentials = scoped_credentials)
感谢您的帮助和思考:-)

关于python - 如何从 python 函数登录 Google Cloud Storage?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63561032/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com