gcp 存储桶对象的签名 URL 因访问被拒绝而失败

问题描述

当我为存储 gcp 存储桶中的对象生成带有服务帐户的签名下载 url 时,我希望任何人无需身份验证即可使用它。但是,我不断收到“匿名调用者没有 storage.objects.get 访问 Google Cloud Storage 对象的权限”。我做错了什么?

url,err := gcs.SignedURL(bktName,so.Name(),&gcs.SignedURLOptions{
    GoogleAccessID: serviceAccountName,Method:         "GET",Expires:        time.Now().Add(duration),ContentType:    md.RenditionMetadata[0].ContentType,Headers:        []string{fmt.Sprintf("x-goog-Meta-filename: %s",md.RenditionMetadata[0].FileName)},SignBytes: func(b []byte) ([]byte,error) {
        signedBlob,err := iam.SignBlob(s.GoogleIamService(),serviceAccountName,b)
        if err != nil {
            return nil,err
        }
        return []byte(signedBlob),err
    },})

我使用的服务帐户具有 Storage Object Creator 和 Storage Object Viewer 角色...

解决方法

按照创建签名 URL 的指南下载对象: https://cloud.google.com/storage/docs/samples/storage-generate-signed-url-v4

或者,您可以使用 gsutil 命令来创建签名的 URl: https://cloud.google.com/storage/docs/gsutil/commands/signurl

,

如果您在创建签名网址时指定标题, 您必须在“卷曲”生成的 url 时包含它们;-)