gpt4 book ai didi

python - google.cloud 导入存储 : cannot import storage

转载 作者:太空狗 更新时间:2023-10-29 19:36:21 27 4
gpt4 key购买 nike

我试着按照我在这里找到的谷歌教程运行下面的代码:https://cloud.google.com/docs/authentication/production

def implicit():
from google.cloud import storage

# If you don't specify credentials when constructing the client, the
# client library will look for credentials in the environment.
project = 'my_project_name'
storage_client = storage.Client(project=project)

# Make an authenticated API request
buckets = list(storage_client.list_buckets())
print(buckets)

implicit()

但它一直给我以下错误:

Traceback (most recent call last):
File "[PATH]/scratch_5.py", line 13, in <module>
implicit()
File "[PATH]/scratch_5.py", line 2, in implicit
from google.cloud import storage
ImportError: cannot import name storage

有人可以帮我吗?

最佳答案

我看到您正在尝试使用 Google Cloud Storage client libraries .

为了使用它,你应该首先确保它安装在你的机器上:

pip install --upgrade google-cloud-storage

然后,您可能应该通过在运行代码,如下所示。如果您使用的是 Windows,请遵循 Application Default Credentials , 而不是。

export GOOGLE_APPLICATION_CREDENTIALS="/path/to/file.json"

或者,您可以尝试 steps presented in the documentation .您共享的凭证(使用从环境中获取的隐式凭据)与使用显式凭证的唯一区别是,当您声明 GCS 客户端时,您应该执行如下操作:

storage_client = storage.Client.from_service_account_json('/path/to/SA_key.json')

一旦一切准备就绪,运行您提供的示例代码应该没有问题。为了继续了解 GCS 及其客户端库,请随时搜索我链接的文档并查看 using explicit credentials .

关于python - google.cloud 导入存储 : cannot import storage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50840511/

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