将新创建的文件在Google Cloud Storage Go运行时客户端API上公开。

huangapple go评论83阅读模式
英文:

Making Newly created file public on Google Cloud Storage Go runtime Client API

问题

我正在使用Google Cloud Storage Go Runtime客户端API将GO服务器中的字符串写入默认存储桶中的文件。以下是代码示例:

wc := storage.NewWriter(d.ctx, bucket, fileName)
wc.ContentType = "text/plain"
if _, err := wc.Write([]byte(myData)); err != nil {
    d.errorf("createFile: unable to write data to bucket %q, file %q: %v", bucket, fileName, err)
    return
}

但是我无法在wc对象上设置正确的ACL权限,以使文件变为公开。如何实现这一目标,使创建的文件变为公开?

英文:

Im using Google Cloud Storage Go Runtime client API to write a string from GO server into a file in the default bucket . Here is the code

wc := storage.NewWriter(d.ctx, bucket, fileName)
wc.ContentType = "text/plain"
if _, err := wc.Write([]byte(myData)); err != nil {
        d.errorf("createFile: unable to write data to bucket %q, file %q: %v", bucket, fileName, err)
        return
}

But im Not able set right ACL permissions on the wc object to make the file public ?? How to do achieve it so that the created file becomes public ?

答案1

得分: 4

尝试设置:

wc.ACL = []storage.ACLRule{{storage.AllUsers, storage.RoleReader}}

或者,您还可以更改存储桶上的默认对象 ACL,以便新创建的对象默认为公开可读取。

英文:

Try setting:

wc.ACL = []storage.ACLRule{{storage.AllUsers, storage.RoleReader}}

Alternately, you can also change the default object ACL on the bucket so that newly created objects default to being publicly readable.

答案2

得分: 1

你可以通过以下方式完成相同的操作。

acl := client.Bucket(bucket).Object(object).ACL()
if err := acl.Set(ctx, storage.AllUsers, storage.RoleReader); err != nil {
        return err
}

这是文档的链接:这里

英文:

You can do the same by this also.

acl := client.Bucket(bucket).Object(object).ACL()
if err := acl.Set(ctx, storage.AllUsers, storage.RoleReader); err != nil {
        return err
}

Here is the link to the docs.

huangapple
  • 本文由 发表于 2015年3月25日 13:01:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/29247826.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定