首页 > 解决方案 > 使用服务帐户json文件通过python推送GCP Cloud Storage文件

问题描述

我根据 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 函数的行中收到上述错误。这是我在谷歌文档中找到的。

文档中的所有示例代码都有 get_bucket 函数来上传文件。我的问题是如何在没有此访问权限的情况下将文件上传到存储桶(storage.buckets.get)?因为我们在没有这个访问权限的情况下通过 Java 上传到同一个存储桶。

你能对此有所了解吗?请

标签: python-3.xgoogle-cloud-platformgoogle-cloud-storage

解决方案


您使用的服务帐户没有适当的权限

您可以通过至少授予roles/storage.objectAdmin存储桶或项目级别的角色来解决此问题。

roles/storage.objectAdmin 角色:_

授予对对象的完全控制权,包括列出、创建、查看和删除对象。

在存储桶级别授予它,请运行:

gsutil iam ch serviceAccount:someid-sit@someinfo-sit.iam.gserviceaccount.com:roles/storage.objectAdmin gs://[BUCKET_NAME]

在项目级别运行授予它:

gcloud projects add-iam-policy-binding yourProject --member serviceAccount:someid-sit@someinfo-sit.iam.gserviceaccount.com --role roles/storage.objectAdmin

编辑

您需要将凭据传递给storage_client

storage_client = storage.Client('proj-sit', credentials=credentials)


推荐阅读