google 已签名存储 url public 已读取

google storage signed url public read

我正在尝试为 public 图片创建签名 URL
是否可以在 URL 上设置权限级别,还是需要在文件上传后设置?

我试图通过传递一些 header 来做到这一点,但我不确定正确的方法。

opts := &storage.SignedURLOptions{
    GoogleAccessID: googleAccessID,
    PrivateKey:     data,
    Method:         "PUT",
    Expires:        time.Now().Add(time.Hour * 1),
    ContentType:    r.MimeType,
    Headers:        []string{"x-goog-acl"},
or maybe Headers:        []string{"x-goog-acl:public-read"},

}

那客户端也需要设置header?

创建新的bucket writer时即可。您可以设置 headers 并创建 ACLRule。我在 Google App Engine 上使用 "google.golang.org/cloud/storage" 包托管的应用程序中执行此操作。希望这有帮助。

wc := bucket.Object(filename).NewWriter(ctx)
wc.ContentType = contentType
wc.CacheControl = "public, max-age=86400"
wc.ObjectAttrs.ACL = append(wc.ObjectAttrs.ACL, storage.ACLRule{Role: storage.RoleReader, Entity: storage.AllUsers})

使用已签名 URL 的人将获得签署 URL 的实体的许可。假设您创建服务帐户 A 并使用 A 的私钥签署 URL。当最终用户尝试使用该 URL 获取资源时,GCS 将检查服务帐户 A 是否具有读取权限。