Use Go SDK V2 to set and retrieve the access control list (ACL) of an OSS bucket.
ACLs control who can read or write objects in a bucket. For more granular access control at the user or role level, use RAM policies instead.
ACL types
OSS supports three bucket ACL types:
| ACL | Access granted | Go SDK constant |
|---|---|---|
| Private | Owner and authorized users: full control. Others: no access. | oss.BucketACLPrivate |
| Public-read | Owner and authorized users: full control. Others: read-only. | oss.BucketACLPublicRead |
| Public-read-write | Owner: full control. Others: read and write. | oss.BucketACLPublicReadWrite |
Public-read and Public-read-write make bucket objects accessible to anyone on the internet, including anonymous users. Avoid these settings unless you intentionally serve public content.
Prerequisites
Before you begin, ensure that you have:
An OSS bucket
The
oss:PutBucketAclpermission to set a bucket ACLThe
oss:GetBucketAclpermission to retrieve a bucket ACL
For information about granting these permissions to RAM users, see Grant custom policy to RAM users.
Usage notes
The sample code uses the region ID
cn-hangzhou. Replace it with the region where your bucket is located. For a full list of regions and endpoints, see OSS regions and endpoints.The sample code reads access credentials from environment variables. For other credential options, see Configure access credentials.
To access OSS from another Alibaba Cloud service in the same region, use an internal endpoint instead of the default public endpoint.
Set and retrieve a bucket ACL
The following example sets the bucket ACL to Private, then retrieves the ACL to confirm the change.
package main
import (
"context"
"flag"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
var (
region string
bucketName string
)
func init() {
flag.StringVar(®ion, "region", "", "The region where the bucket is located.")
flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}
func main() {
flag.Parse()
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
client := oss.NewClient(cfg)
// Set the bucket ACL to Private.
putResult, err := client.PutBucketAcl(context.TODO(), &oss.PutBucketAclRequest{
Bucket: oss.Ptr(bucketName),
Acl: oss.BucketACLPrivate,
})
if err != nil {
log.Fatalf("failed to set bucket ACL: %v", err)
}
log.Printf("PutBucketAcl result: %#v\n", putResult)
// Retrieve the bucket ACL.
getResult, err := client.GetBucketAcl(context.TODO(), &oss.GetBucketAclRequest{
Bucket: oss.Ptr(bucketName),
})
if err != nil {
log.Fatalf("failed to get bucket ACL: %v", err)
}
log.Printf("GetBucketAcl result: %#v\n", getResult)
}References
Complete sample code for setting a bucket ACL: GitHub sample
API reference for
PutBucketAcl: pkg.go.devComplete sample code for retrieving a bucket ACL: GitHub sample
API reference for
GetBucketAcl: pkg.go.dev