Manage object ACLs (Go SDK V2)
Manage the access control lists (ACLs) of objects in a versioning-enabled bucket.
Notes
-
The sample code in this topic uses the China (Hangzhou) region, which has the region ID
cn-hangzhou. By default, a public endpoint is used to access resources in a bucket. To access resources from other Alibaba Cloud services in the same region as the bucket, use an internal endpoint. For more information about the regions and endpoints supported by Object Storage Service (OSS), see OSS regions and endpoints. -
The examples in this topic read access credentials from environment variables. For more information, see Configure access credentials.
-
To set the ACL of an object, you must have the
oss:PutObjectAclpermission. To query the ACL of an object, you must have theoss:GetObjectAclpermission. For more information, see Grant custom permissions to RAM users.
Types of ACLs
The following table describes the four ACLs that you can set for an object:
|
ACL |
Description |
ACL value |
|
Inherit from bucket |
Files inherit the access permissions of the bucket. |
oss.ObjectACLDefault |
|
Private |
Only the object owner and authorized users have read and write permissions on the object. Other users cannot access the object. |
oss.ObjectACLPrivate |
|
Public-read |
The object owner and authorized users have read and write permissions on the object. Other users have only read permissions. Exercise caution when you grant this permission. |
ObjectACLPublicRead |
|
Public-read-write |
All users have read and write permissions on the object. Exercise caution when you grant this permission. |
oss.ObjectACLPublicReadWrite |
The ACL of an object takes precedence over the ACL of the bucket. For example, if the bucket ACL is private and the object ACL is public-read-write, all users have read and write permissions on the object. If you do not set an ACL for an object, the object inherits the bucket ACL.
Sample code
-
By default, the PutObjectAcl operation sets the ACL of the current version of an object. If the current version of the object is a delete marker, OSS returns 404 Not Found. You can specify a version ID in the request to set the ACL of a specific version of the object.
-
By default, the GetObjectAcl operation queries the ACL of the current version of an object. If the current version of the object is a delete marker, OSS returns 404 Not Found. You can specify a version ID in the request to query the ACL of a specific version of an object.
The following sample code sets and queries the ACL of an object.
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"
)
// Define global variables.
var (
region string // The region in which the bucket is located.
bucketName string // The name of the bucket.
objectName string // The name of the object.
)
// The init function is used to initialize command-line parameters.
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
flag.StringVar(&objectName, "object", "", "The name of the object.")
}
func main() {
// Parse command-line parameters.
flag.Parse()
// Check whether the region is empty.
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
// Check whether the bucket name is empty.
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
// Check whether the object name is empty.
if len(objectName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, object name required")
}
// Load the default configurations and set the credential provider and region.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
// Create an OSS client.
client := oss.NewClient(cfg)
// Create a request to set the object ACL.
putRequest := &oss.PutObjectAclRequest{
Bucket: oss.Ptr(bucketName), // The name of the bucket.
Key: oss.Ptr(objectName), // The name of the object.
VersionId: oss.Ptr("yourVersionId"), // Specify the actual version ID.
Acl: oss.ObjectACLPrivate, // Set the ACL of the object to private.
}
// Execute the operation to set the object ACL.
putResult, err := client.PutObjectAcl(context.TODO(), putRequest)
if err != nil {
log.Fatalf("failed to put object acl %v", err)
}
// Print the result of setting the object ACL.
log.Printf("put object acl result:%#v\n", putResult)
// Create a request to query the object ACL (access control list).
getRequest := &oss.GetObjectAclRequest{
Bucket: oss.Ptr(bucketName), // The name of the bucket.
Key: oss.Ptr(objectName), // The name of the object.
VersionId: oss.Ptr("yourVersionId"), // Specify the actual version ID.
}
// Execute the operation to query the object ACL.
getResult, err := client.GetObjectAcl(context.TODO(), getRequest)
if err != nil {
log.Fatalf("failed to get object acl %v", err)
}
// Print the result of querying the object ACL.
log.Printf("get object acl result:%#v\n", getResult)
}
References
-
For the complete sample code for setting the ACL of an object, see GitHub example.
-
For more information about the API operation for setting the ACL of an object, see PutObjectAcl.
-
For the complete sample code for querying the ACL of an object, see GitHub example.
-
For more information about the API operation for querying the ACL of an object, see GetObjectAcl.