google.cloud导入存储:无法导入存储

google.cloud导入存储:无法导入存储

问题描述:

我尝试按照我在此处找到的Google教程来运行以下代码: https://cloud.google.com/docs/authentication/production

I tried to run the code bellow by following the google tutorials i found here: 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()

但是它一直给我以下错误:

But it keeps giving me the following error:

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

有人可以帮我吗?

我看到您正在尝试使用

I see you are trying to use the Google Cloud Storage client libraries.

要使用它,首先应确保已将其安装在计算机中:

In order to use it, you should first make sure that it is installed in your machine:

pip install --upgrade google-cloud-storage

然后,您可能应该设置身份验证(如果您使用的是),方法是在运行代码的计算机上设置GOOGLE_APPLICATION_CREDENTIALS环境变量,如下所示.如果您使用的是Windows,请按照文档中提供的步骤进行操作

And then, you should probably set up authentication (if you are using Application Default Credentials, from the documentation you mentioned), by setting up the GOOGLE_APPLICATION_CREDENTIALS environment variable in the machine where you are running the code, like below. If you are using Windows, follow the steps presented in the documentation, instead.

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

或者,您可以尝试使用显式凭据 .共享(使用从环境中获取的隐式凭据)和使用显式凭据的共享之间的唯一区别是,在声明GCS客户端时,您应该执行以下操作:

Alternatively, you can try using explicit credentials. The only difference between the one you shared (using implicit credentials obtained from the environment) and one using explicit credentials, is that when you declare the GCS client, you should do something like:

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

所有这些准备就绪后,运行所提供的示例代码就不会有问题.为了不断了解GCS及其客户端库,请随时搜索我链接的文档,并查看

Once all this is ready, you should have no issues with running the sample code you provided. In order to keep learning about GCS and its client libraries, feel free to search on the documentation I linked and have a look at the library reference page.