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

Sample code

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

package main

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

func main() {
    // Create an OSSClient instance. 
    // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. 
    // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. 
    client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Create a bucket named examplebucket of which the storage class is IA (oss.StorageIA), the access control list (ACL) is public-read (oss.ACLPublicRead), and the redundancy type is zone-redundant storage (oss. Redundant ZRS). 
    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)
    }
}

References

  • For the complete sample code that is used to create a bucket, visit GitHub.
  • For the API operation that you can call to create a bucket, see PutBucket.