英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论