All Products
Search
Document Center

Object Storage Service:Create a bucket (Go SDK V2)

Last Updated:Mar 20, 2026

A bucket is a container for storing objects in OSS.

Prerequisites

Before you begin, make sure you have:

  • Configured access credentials. The examples in this topic read credentials from environment variables. For setup instructions, see Configure access credentials.

  • The oss:PutBucket permission. By default, an Alibaba Cloud account has full permissions. RAM users and RAM roles have no permissions by default — grant them through a RAM Policy or Bucket Policy.

Usage notes

  • The sample code uses the region ID cn-hangzhou (China (Hangzhou)). By default, the public endpoint is used. To access bucket resources from another Alibaba Cloud service in the same region, use the internal endpoint instead. For region-to-endpoint mappings, see Regions and endpoints.

  • Starting October 13, 2025 at 10:00 (UTC+8), OSS enables Block Public Access by default for all new buckets created via the API, OSS SDKs, or ossutil. The rollout is phased across regions — see the official announcement for the schedule. When Block Public Access is enabled, you cannot set public ACLs (public read or public read/write) or bucket policies that allow public access. To allow public access, disable this feature after the bucket is created.

Permissions

APIActionDescription
PutBucketoss:PutBucketCreates a bucket.
oss:PutBucketAclModifies the bucket ACL after creation.

Method definition

func (c *Client) PutBucket(ctx context.Context, request *PutBucketRequest, optFns ...func(*Options)) (*PutBucketResult, error)

Request parameters

ParameterTypeDescription
ctxcontext.ContextContext of the request. Use this to set the total request timeout.
request*PutBucketRequestRequest parameters. See PutBucketRequest.
optFns...func(*Options)(Optional) Operation-level configuration. See Options.

Response parameters

ParameterTypeDescription
result*PutBucketResultThe return value. Valid only when err is nil. See PutBucketResult.
errerrorThe request status. Non-nil when the request fails.

Examples

The following example creates a bucket. Pass --region and --bucket as command-line flags.

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(&region, "region", "", "The region in which 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")
	}

	// Load default config. Credentials are read from environment variables.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	// Create an OSS client.
	client := oss.NewClient(cfg)

	request := &oss.PutBucketRequest{
		Bucket: oss.Ptr(bucketName),
	}

	// Create the bucket.
	result, err := client.PutBucket(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to put bucket %v", err)
	}

	log.Printf("put bucket result:%#v\n", result)
}

For the complete sample, see put_bucket.go on GitHub.

What's next

After creating a bucket, you can:

  • Upload objects — Store files in the bucket using PutObject or multipart upload.

  • Set the storage class — Reduce costs by choosing Standard, Infrequent Access, Archive, or Cold Archive storage.

  • Configure a lifecycle rule — Automatically transition or delete objects based on age or storage class.

  • Control access — Restrict or grant access using RAM Policies, bucket policies, or Access Control Lists (ACLs).

References