A bucket is a container for objects stored in OSS. Every object is contained in a bucket. This topic describes how to create a bucket.

For the complete code used to create a bucket, visit GitHub.

The following code provides an example on how to create a bucket:

package main

    import (
        "fmt"
        "os"
        "github.com/aliyun/aliyun-oss-go-sdk/oss"
    )

func main() {
    // Create an OSSClient instance.
    client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
    if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Create a bucket (the default storage class is Standard) and set the ACL of the bucket to public read (the default ACL is private).
    err = client.CreateBucket("<yourBucketName1>", oss.ACL(oss.ACLPublicRead))
    if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Create the bucket and set the type of disaster recovery to zone-redundant storage.
    err = client.CreateBucket("<yourBucketName2>", oss.RedundancyType(oss.RedundancyZRS))
    if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
}

For more information about the bucket naming conventions, see Bucket.

You can specify the ACL and storage class when you create a bucket. For more information, see Set the ACL for a bucket and Overview of storage classes.

The following code provides an example on how to create a bucket of the Archive or Infrequent Access (IA) storage class:

// Create a bucket of the Archive storage class. To create a bucket of the IA storage class, replace oss.StorageArchive with oss.StorageIA.
    err = client.CreateBucket("<yourBucketName>", oss.StorageClass(oss.StorageArchive))
    if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

For more information about how to create a bucket, see PutBucket.