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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50840511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:36:51  来源:igfitidea点击:

google.cloud import storage: cannot import storage

pythongoogle-cloud-platformgoogle-cloud-storage

提问by Mariane Reis

I tried to run the code bellow by following the google tutorials i found here: https://cloud.google.com/docs/authentication/production

我尝试按照我在此处找到的谷歌教程来运行以下代码: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

Could someone help me with this?

有人可以帮我解决这个问题吗?

回答by dsesto

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

我看到您正在尝试使用Google Cloud Storage 客户端库

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

为了使用它,您应该首先确保它已安装在您的机器中:

pip install --upgrade google-cloud-storage

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_CREDENTIALSenvironment 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.

然后,您可能应该通过在运行代码的机器中设置环境变量来设置身份验证(如果您使用的是Application Default Credentials,则来自您提到的文档),GOOGLE_APPLICATION_CREDENTIALS如下所示。如果您使用的是 Windows,请按照文档中提供步骤操作

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

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:

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

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

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.

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

回答by Gidi9

Also, make sure your main.pyfile and the requirements.txtare in the same directory and the same directory as the function being deployed.

此外,请确保您的main.py文件 和 与requirements.txt正在部署的函数位于同一目录和同一目录中。

Just FYI, because I had to do this even after specifying my environment variables.

仅供参考,因为即使在指定了环境变量之后我也必须这样做。