【问题标题】:GCS Python access from colab从 colab 访问 GCS Python
【发布时间】:2019-10-01 03:39:54
【问题描述】:

我正在尝试使用以下代码行从 Colab 访问 GCS 并得到给定的错误。我错过了什么吗?还是 Colab 不支持这种 GCS 访问?我可以使用任何解决方法或最佳做法吗?

from google.cloud import storage

client = storage.Client()
bucket = client.get_bucket('busnet_videos')
blob = bucket.blob('my-test-file.txt')
blob.upload_from_string('this is test content!')

错误:

    ---------------------------------------------------------------------------

OSError                                   Traceback (most recent call last)

<ipython-input-7-0ed440d78c8f> in <module>()
      7 from google.cloud import storage
      8 
----> 9 client = storage.Client()
     10 bucket = client.get_bucket('busnet_videos')
     11 blob = bucket.blob('my-test-file.txt')

2 frames

/usr/local/lib/python3.6/dist-packages/google/cloud/storage/client.py in __init__(self, project, credentials, _http)
     71             project = None
     72         super(Client, self).__init__(
---> 73             project=project, credentials=credentials, _http=_http
     74         )
     75         if no_project:

/usr/local/lib/python3.6/dist-packages/google/cloud/client.py in __init__(self, project, credentials, _http)
    221 
    222     def __init__(self, project=None, credentials=None, _http=None):
--> 223         _ClientProjectMixin.__init__(self, project=project)
    224         Client.__init__(self, credentials=credentials, _http=_http)

/usr/local/lib/python3.6/dist-packages/google/cloud/client.py in __init__(self, project)
    176         if project is None:
    177             raise EnvironmentError(
--> 178                 "Project was not passed and could not be "
    179                 "determined from the environment."
    180             )

OSError: Project was not passed and could not be determined from the environment.

【问题讨论】:

  • 在实例化客户端时尝试指定项目 ID:client = storage.Client("your-project-id")。我在this github issue看到它。

标签: google-cloud-storage google-colaboratory


【解决方案1】:

你可能需要设置一个环境变量:

GOOGLE_APPLICATION_CREDENTIALS=SERVICE_ACCOUNT_KEY.json

PROJECT_ID=YOUR_GOOGLE_CLOUD_PROJECT_ID

【讨论】:

    猜你喜欢
    • 2015-03-11
    • 1970-01-01
    • 1970-01-01
    • 2019-05-08
    • 2017-02-12
    • 1970-01-01
    • 2015-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多