如何为 GCP 服务帐户 storage.buckets.list 授予只读访问权限?

How do I give a GCP service account storage.buckets.list access with read only access?

我正在尝试做 gsutil ls 但是结果是:

ubuntu@ip:~$ gsutil ls
AccessDeniedException: 403 xxxxxxxxxxxx@xxxxxxxxxx.iam.gserviceaccount.com does not have storage.buckets.list access to project xxxxxxxxxxxxxxx.

我可以只 读取/查看访问 IAM 角色授予此权限吗?

当然可以。至少,您始终可以创建一个 custom role with exactly the permissions you want. You do this by clicking the Create Role button at the top of the roles tab. Then, once it is created, apply that role to your service account on the IAM page,就像任何其他角色一样。

或者,您可以使用云控制台中的 same roles tab 明确搜索该权限,以查看哪些角色包含该权限,看看是否有适合您的权限。

但是,在这种情况下,我没有看到明显的默认限制。也就是说,在这种情况下,您可以将 Storage Legacy Bucket Reader (roles/storage.legacyBucketReader) 作为自定义角色的起点——如果您在角色选项卡上 select 这个角色,您可以 'Create Role from Selection' 将其用作起点)。

命令 gsutil ls 列出项目中的存储桶。

要列出存储桶,您需要 storage.buckets.list.

权限

要列出存储桶中的对象,您需要权限 storage.objects.list

这些权限都不允许您读取对象。要读取对象,您需要权限 storage.objects.get.

只读取一个对象,不需要列表权限。但是,由于您使用的是 gsutil 命令,所以您可以这样做。

您可以将多个预定义角色附加到您的服务帐户,以授予 gsutil 的必要权限。

推荐:

roles/storage.objectViewer

或以下两个角色:

roles/storage.legacyObjectReader
roles/storage.legacyBucketReader

如果您只想分配一个角色来读取对象而不列出它们:

roles/storage.legacyObjectReader

截至 2022 年 1 月对 GCP GCS 存储桶的只读权限:

storage.buckets.get
storage.buckets.list
storage.objects.get
storage.objects.list