gpt4 book ai didi

python-3.x - python 使用服务帐户 json 文件推送 GCP 云存储文件

转载 作者:行者123 更新时间:2023-12-03 20:40:14 25 4
gpt4 key购买 nike

我已经根据 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 上传到同一个存储桶。

你能说明一下吗?请

最佳答案

您使用的服务帐户没有正确的 permissions .

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

The roles/storage.objectAdmin role:

Grants full control over objects, including listing, creating, viewing, and deleting objects.

收件人grant it at bucket level运行:

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

收件人grant it at project level运行:

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)

关于python-3.x - python 使用服务帐户 json 文件推送 GCP 云存储文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55138374/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com