【发布时间】:2019-08-03 22:08:35
【问题描述】:
我根据 Google 文档编写了简单的 python 程序。它给我一个错误,说给定的帐户没有访问权限。尝试了不同的组合,但没有奏效。
我已经通过提供给 java 程序和 gsutil 来交叉检查给定的访问权限。这两个地方我都可以访问存储桶并上传文件。 python程序的问题。请对这个问题有所了解。
from google.oauth2 import service_account
from google.cloud import storage
credentials = service_account.Credentials.from_service_account_file('C:/Users/AWS/python/sit.json'
,scopes=['https://www.googleapis.com/auth/cloud-platform'])
storage_client = storage.Client(credentials=credentials,project='proj-sit')
bucket = storage_client.get_bucket('b-sit')
blob = bucket.blob('myfile')
blob.upload_from_string('New contents!. This is test.')
我收到以下错误
Traceback (most recent call last):
File "C:\Users\AWS\python\mypgm.py", line 21, in <module>
bucket = storage_client.get_bucket('pearson-gcss-sit') # pearson-gcss-sit pearson-bbi-dev global-integration-nonprod
File "D:\Development_Avecto\Python36\lib\site-packages\google\cloud\storage\client.py", line 227, in get_bucket
bucket.reload(client=self)
File "D:\Development_Avecto\Python36\lib\site-packages\google\cloud\storage\_helpers.py", line 106, in reload
method="GET", path=self.path, query_params=query_params, _target_object=self
File "D:\Development_Avecto\Python36\lib\site-packages\google\cloud\_http.py", line 319, in api_request
raise exceptions.from_http_response(response)
google.api_core.exceptions.Forbidden: 403 GET https://www.googleapis.com/storage/v1/b/b-sit?projection=noAcl: someid-sit@someinfo-sit.iam.gserviceaccount.com does not have storage.buckets.get access to b-sit.
[Finished in 10.6s]
注意:我可以在 console.cloud.google.com 中看到角色为“storage.objectAdmin”。
更多信息,我可以使用下面的java程序上传文件。
GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(connectionKeyPath))
.createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));
Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();
BlobId blobId = BlobId.of("some-sit", "cloudDirectory/file.zip");
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("application/zip").build();
Blob blob = storage.create(blobInfo, fileContent);
我找到了问题的根本原因。
根本原因:我的存储桶可以访问“roles/storage.objectAdmin”,而无法访问“storage.buckets.get”。因此,我在具有 get_bucket 函数的行中收到上述错误。这是我在谷歌documentation找到的。
文档中的所有示例代码都有 get_bucket 函数来上传文件。我的问题是我们如何在没有此访问权限的情况下将文件上传到存储桶(storage.buckets.get)?因为我们在没有这个权限的情况下通过Java上传到同一个bucket。
您能对此有所了解吗?请
【问题讨论】:
标签: python-3.x google-cloud-platform google-cloud-storage