Cloud Build 服务帐户无权访问 storage.objects.get

Cloud Build service account no access to storage.objects.get

我正在尝试获取 Google Cloud Functions 以从存储桶中的文件打印内容。 我将文件存储在一个存储桶中,一个经过身份验证的服务帐户 Storage AdminCloud Run AdminService Account UserCloud Functions Admin 以及以下 python 脚本。

def from_storage(event, context):
    import json
    from google.cloud import storage

    client = storage.Client(project='my-project')
    try:
        bucket = client.get_bucket('my-storage')
    except Exception as e:
        print('Bucket not found.')
        print(e)
    try:
        blob = bucket.blob('Hello_World.json')
        data = json.load(blob.download_as_string())
        return data
    except Exception as e:
        print('Error loading file:')
        print(e)

我尝试使用以下代码进行部署:

gcloud functions deploy from_storage --runtime python39 --triger-http --allow-unauthenticated

我收到一条错误消息,即部署服务帐户(似乎是自动创建的服务帐户)没有 storage.objects.get 权限:

ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed: could not resolve storage source: googleapi: Error 403: 488395598433@cloudbuild.gserviceaccount.com does not have storage.objects.get access to the Google Cloud Storage object., forbidden

我觉得这很奇怪,因为我在我的 IAM 中没有看到这个服务帐户,也无法在 Cloud Functions 中访问它的权限。任何帮助将不胜感激!

如果您在其他服务帐户中看不到“xxxxxxxxxxxx@cloudbuild.gserviceaccount.com”。因此,我认为在激活 Cloud Build API 时出了点问题。能否请您禁用 Cloud Build API 然后再次启用它以查看它是否解决了问题?

如果重新启用 Cloud Build 后 API 您仍然看不到它的服务帐户,请通过“IAM & Admin”->“IAM”页面手动添加它,授予它 Cloud Build服务帐户角色。

我猜 488395598433@cloudbuild.gserviceaccount.com - 是一个 Cloud Build 服务帐户,其中前缀(数字)是项目的编号,其中 Cloud Build 是 运行ning。请检查是否启用了 Cloud Build API?请问Cloud Build服务账号有相关权限吗?

运行期间,云函数默认在PROJECT_ID@appspot.gserviceaccount.com服务账号下运行ning,其中PROJECT_ID为项目ID,部署该云功能的位置(并且应该 运行)。这是 App Engine 默认服务帐户。

可以(并建议)根据最小权限原则创建专用服务帐户。在这种情况下,您可以使用相应的参数部署云函数(不是在您的示例中)。

根据我对您的特定示例的最佳理解,将使用默认的 App Engine 服务帐户。

在任何一种情况下,云功能 运行 时间服务帐户应具有相关权限 (IAM permissions/roles) 以使用 APIs 和资源(在任何项目中)。

请您检查一下云函数运行时间服务帐号是否有访问云存储桶的相关权限,好吗?请记住,云功能可能部署到一个项目中,而存储桶可能在另一个项目中。

在您的 post 中,您提到您有一个“经过身份验证的服务帐户,具有”一些权限。如果您使用默认的 App Engine 帐户部署服务帐户,那么该服务帐户有什么用?可能我错过了什么。 “为了使用非默认服务帐户部署功能,部署者必须对正在部署的服务帐户具有 iam.serviceAccounts.actAs 权限。” - 来自 Permissions required to use non-default identities 如果您是这种情况 - 您也可以检查一下吗?

参考:
Configure Access for Cloud Build Service Account and this link 解释了自动创建的 Cloud Build 服务帐户的起始权限(在自动创建时授予)。

根据我的经验和我猜测幕后发生的事情,我遇到了这个错误,你也可能遇到过,因为你是第一次使用 Cloud Build 服务。所以我在我的脚本的第一个 运行 上遇到了错误,但是在随后的 运行 上 错误消失了 。这意味着在服务器上授予权限需要时间。