All Products
Search
Document Center

Object Storage Service:Manage bucket access control lists (Go SDK V1)

Last Updated:Nov 29, 2025

A bucket is a container for objects stored in Object Storage Service (OSS). All objects in OSS are stored in buckets. This topic describes how to configure and query the access control list (ACL) of a bucket.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.

  • In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Configure OSSClient instances.

  • To configure the ACL of a bucket, you must have the oss:PutBucketAcl permission. To query the ACL of a bucket, you must have the oss:GetBucketAcl permission. For more information, see Attach a custom policy to a RAM user.

Bucket ACL types

The following table describes the bucket ACLs.

ACL

Description

Method

Private

Only the bucket owner and authorized users have read and write permissions on objects in the bucket. Other users have no permissions.

oss.ACLPrivate

Public-read

The bucket owner and authorized users have read and write permissions on objects in the bucket. Other users have only read permissions. Use this permission with caution.

oss.ACLPublicRead

Public-read-write

All users have read and write permissions on objects in the bucket. Use this permission with caution.

oss.ACLPublicReadWrite

Sample code

The following code shows how to set and retrieve the ACL of a bucket:

package main

import (
	"log"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// Create an OSSClient instance.
	// Set yourEndpoint to the endpoint of the bucket. For example, for the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
	// Set yourRegion to the region where the bucket is located. For example, for the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("yourRegion"))
	// Set the signature version.
	clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("yourEndpoint", "", "", clientOptions...)
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Set yourBucketName to the name of the bucket.
	bucketName := "yourBucketName"

	// Set the bucket ACL to public-read.
	err = client.SetBucketACL(bucketName, oss.ACLPublicRead)
	if err != nil {
		log.Fatalf("Failed to set bucket ACL for '%s': %v", bucketName, err)
	}

	// Get the bucket ACL.
	aclRes, err := client.GetBucketACL(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket ACL for '%s': %v", bucketName, err)
	}

	log.Printf("Bucket ACL for '%s': %s", bucketName, aclRes.ACL)
}

References

  • For the complete sample code for bucket ACLs, see GitHub sample.

  • For more information about the API operation used to set bucket ACLs, see SetBucketACL.

  • For more information about the API operation used to retrieve bucket ACLs, see GetBucketACL.