A bucket is a container for storing objects. All objects must be stored in a bucket. This topic describes how to create a bucket.
Prerequisites
The
oss:PutBucketpermission is granted. For more information, see Attach a custom policy to a RAM user.Access credentials are configured as environment variables. For more information, see Configure access credentials.
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.
This topic demonstrates creating an OSSClient instance with an OSS endpoint. For alternative configurations, such as using a custom domain or authenticating with credentials from Security Token Service (STS), see Configure a client (Go SDK V1).
Starting October 13, 2025, at 10:00 (UTC+8), OSS begins a phased rollout to enable Block Public Access by default for all new buckets. This change applies to buckets created through the API, SDKs, and ossutil. For the specific rollout schedule in each region, see the Notice. When this feature is enabled, you cannot grant public access to the bucket, either through ACLs (such as public-read or public-read-write) or bucket policies. If your use case requires public access, disable this setting after the bucket is created.
Bucket configuration options
The following table describes the optional configuration parameters that you can specify when you create a bucket.
| Parameter | Method | Available values | Default | Description |
|---|---|---|---|---|
| Storage class | oss.StorageClass() | oss.StorageStandard, oss.StorageIA, oss.StorageArchive, oss.StorageColdArchive | Standard | The storage class of the bucket. |
| ACL | oss.ACL() | oss.ACLPrivate, oss.ACLPublicRead, oss.ACLPublicReadWrite | Private | The access control list (ACL) of the bucket. |
| Redundancy type | oss.RedundancyType() | oss.RedundancyLRS, oss.RedundancyZRS | LRS | The data redundancy type. LRS stores multiple copies within a single zone. ZRS distributes data across three zones in the same region for higher availability. |
Examples
Create a bucket with default settings
The following code creates a bucket named examplebucket with default settings. By default, the storage class is Standard, the ACL is private, and the redundancy type is locally redundant storage (LRS).
package main
import (
"fmt"
"os"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint.
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("cn-hangzhou"))
// Specify the version of the signature algorithm.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("https://oss-cn-hangzhou.aliyuncs.com", "", "", clientOptions...)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create a bucket named examplebucket with default settings.
err = client.CreateBucket("examplebucket")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
fmt.Println("Bucket created successfully.")
}Create a bucket with specific storage class, ACL, and redundancy type
The following code creates a bucket named examplebucket with the storage class set to Infrequent Access (IA), the ACL set to public-read, and the redundancy type set to zone-redundant storage (ZRS).
package main
import (
"fmt"
"os"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint.
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("cn-hangzhou"))
// Specify the version of the signature algorithm.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("https://oss-cn-hangzhou.aliyuncs.com", "", "", clientOptions...)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create a bucket named examplebucket.
// Set the storage class to Infrequent Access (oss.StorageIA), the ACL to public-read (oss.ACLPublicRead), and the redundancy type to ZRS (oss.RedundancyZRS).
err = client.CreateBucket("examplebucket", oss.StorageClass(oss.StorageIA), oss.ACL(oss.ACLPublicRead), oss.RedundancyType(oss.RedundancyZRS))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
fmt.Println("Bucket created successfully.")
}