我是谷歌云存储的新手,我尝试设置一个每天下载一次 blob 的功能。目前我正在使用我的 Jupyter Notebook,但最后,代码将在 Azure 函数中运行。我正在努力设置将我连接到存储桶的客户端。我有一个服务帐户凭据 JSON,它使我能够连接到谷歌。
在本地,我找到了解决方案:
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
我不知道我做错了什么,因为我认为我已经明确设置了凭据。
最佳P