A bucket is a container that stores objects. Files that you upload are stored as objects in buckets. This topic describes how to create a bucket.
Usage notes
The sample code in this topic uses the region ID
cn-hangzhoufor the China (Hangzhou) region. By default, the public endpoint is used to access resources in a bucket. If you want to access resources in the bucket using other Alibaba Cloud services in the same region as the bucket, use the internal endpoint. For more information about the mappings between 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.
From 10:00 (UTC+8) on October 13, 2025, OSS will implement a phased adjustment across all regions to enable Block Public Access by default for new buckets created using the API, OSS SDKs, or ossutil. For details about the exact times when the adjustment will take effect in each region, see [Official Announcement] Adjustment to Public Access Blocking Configurations for Newly Created Buckets. Once Block Public Access is enabled, you cannot configure public access permissions, including public ACLs (public read and public read/write) and bucket policies that allows public access. You can disable this feature after the bucket is created if your business requires public access.
Permissions
By default, an Alibaba Cloud account has full permissions. RAM users or RAM roles under an Alibaba Cloud account do not have any permissions by default. The Alibaba Cloud account or account administrator must grant operation permissions through RAM Policy or Bucket Policy.
API | Action | Definition |
PutBucket |
| Creates a bucket. |
| After creating a bucket, this permission is required to modify the bucket ACL. |
Method definition
func (c *Client) PutBucket(ctx context.Context, request *PutBucketRequest, optFns ...func(*Options)) (*PutBucketResult, error)Request parameters
Parameter | Type | Description |
ctx | context.Context | The context of the request. You can use this parameter to specify the total timeout period of the request. |
request | *PutBucketRequest | The request parameters of the operation. For more information, see PutBucketRequest. |
optFns | ...func(*Options) | Optional. The operation-level configuration parameters. For more information, see Options. |
Response parameters
Response parameter | Type | Description |
result | *PutBucketResult | The return value of the operation. This parameter is valid only when the value of err is nil. For more information, see PutBucketResult. |
err | error | The status of the request. If the request fails, the value of err is not nil. |
Examples
You can use the following code to create a bucket.
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"
)
// Specify the global variables.
var (
region string // The region.
bucketName string // The name of the bucket.
)
// Specify the init function 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.")
}
func main() {
// Parse command line parameters.
flag.Parse()
// Check whether the bucket name is empty.
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
// Check whether the region is empty.
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
// Load the default configurations and specify the credential provider and region.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
// Create an OSS client.
client := oss.NewClient(cfg)
request := &oss.PutBucketRequest{
Bucket: oss.Ptr(bucketName), // The name of the bucket.
}
// Send a request to create a bucket.
result, err := client.PutBucket(context.TODO(), request)
if err != nil {
log.Fatalf("failed to put bucket %v", err)
}
// Display the result of the bucket creation.
log.Printf("put bucket result:%#v\n", result)
}
References
For the complete sample code that is used to create a bucket, see GitHub Example.
For more information about the API operation that you can call to create a bucket, see PutBucket.