python-3.x - 使用服务帐户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 上传到同一个存储桶。
你能对此有所了解吗?请
解决方案
您使用的服务帐户没有适当的权限。
您可以通过至少授予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)
推荐阅读
- elasticsearch - 弹性搜索 - 搜索形成单个单词的单词,没有空格
- javascript - 如何使用服务器端错误更新客户端错误对象
- android - 为实现抽象值强制执行资源注释
- r - 多个变量的条形图
- android - 在 Android Studio 中更改 Google App Script 代码时是否需要更新 Play Store 应用程序?
- bash - v 开关在 bash 函数中不起作用(-v 开关)
- python - Tensorflow:model.fit() 第一个 epoch 需要很长的初始化时间
- xamarin - 使用 aries-mobileagent-xamarin / aries-framework-dotnet 创建凭据请求时出错
- java - Spring Security 错误:没有名为“springSecurityFilterChain”的 bean
- javascript - 如何使用 ES6 为隐藏的输入字段添加值