GSResponseError: GSResponseError: 403 Forbidden while writing to Bucket

GSResponseError: GSResponseError: 403 Forbidden while writing to Bucket

我正在尝试将文件从 Compute Engine 实例写入存储桶(均属于同一个项目)。

我使用服务帐户执行了此处提到的步骤Python Example

但出现以下错误:

GSResponseError: GSResponseError: 403 Forbidden
[Fri Apr 01 07:12:11 2016] [error] <?xml version='1.0' encoding='UTF-8'?><Error><Code>AccessDenied</Code><Message>Access denied.</Message><Details>Provided scope(s) are not authorized</Details></Error>

但是当我尝试使用相同的凭据、代码和存储桶从我的本地计算机写入存储桶时,它会写入文件。

我已尝试向服务帐户授予所有可能的权限,将帐户添加到存储桶。

最常见的原因是创建的 GCE 实例没有必要的云存储范围。

您可以在此处阅读有关范围的信息: https://cloud.google.com/compute/docs/authentication#settingupinstances

具体来说,在这种情况下,实例是使用 https://www.googleapis.com/auth/devstorage.read_only 创建的,不允许创建存储桶。

您可以在此处查看可用范围的完整列表: https://cloud.google.com/storage/docs/authentication#oauth-scopes

除了已接受的答案(编辑队列已满,我无法编辑)。

范围覆盖 IAM roles/permissions:

Access scopes let you restrict which services the VM can access. The restrictions are applied in addition to Identity and Access Management (IAM) policies.

因此,当您要写入存储桶时,您需要确保 VM 没有限制性范围,例如 Google 存储的只读范围。如果作用域明确禁用写入,则拥有对存储桶的 IAM 写入权限是不够的。