edit-icon download-icon

Manage a bucket

Last Updated: Nov 07, 2017

A bucket is a namespace on OSS and a management entity for billing, access control, logging, and other advanced features.

View all buckets

You can use the Client.ListBuckets operation to list all buckets of the current user. You can specify the Prefix parameter to list all buckets whose names contain the specified prefix.

Note: For the example code of ListBuckets, see sample/list_buckets.go.

  1. import (
  2. "fmt"
  3. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  4. )
  5. client, err := oss.New("Endpoint", "AccessKeyId", "AccessKeySecret")
  6. if err != nil {
  7. // HandleError(err)
  8. }
  9. // List buckets. 100 buckets are listed by default.
  10. lsRes, err := client.ListBuckets()
  11. if err != nil {
  12. // HandleError(err)
  13. }
  14. fmt.Println("buckets:", lsRes.Buckets)
  15. // Filter buckets with the specified prefix.
  16. lsRes, err = client.ListBuckets(oss.Prefix("my-bucket"))
  17. if err != nil {
  18. // HandleError(err)
  19. }
  20. fmt.Println("buckets:", lsRes.Buckets)

Create a bucket

Note: For example code of CreateBucket, see sample/create_bucket.go.

Create a bucket using the Client.CreateBucket operation and specify the bucket name:

  1. import "github.com/aliyun/aliyun-oss-go-sdk/oss"
  2. client, err := oss.New("Endpoint", "AccessKeyId", "AccessKeySecret")
  3. if err != nil {
  4. // HandleError(err)
  5. }
  6. err = client.CreateBucket("my-bucket")
  7. if err != nil {
  8. // HandleError(err)
  9. }

If no ACL policy is specified when a bucket is created, the default ACL oss.ACLPrivate applies. You can specify an ACL policy when creating a bucket:

  1. err = client.CreateBucket("my-bucket", oss.ACL(oss.ACLPublicRead))
  2. if err != nil {
  3. // HandleError(err)
  4. }

Note:

  • For bucket naming conventions, see Basic OSS Concepts.
  • The bucket name has to be globally unique. Make sure, your bucket name is different from the other users’.

Delete a bucket

You can use the Client.DeleteBucket operation to delete a bucket and specify the bucket name:

  1. import "github.com/aliyun/aliyun-oss-go-sdk/oss"
  2. client, err := oss.New("Endpoint", "AccessKeyId", "AccessKeySecret")
  3. if err != nil {
  4. // HandleError(err)
  5. }
  6. err = client.DeleteBucket("my-bucket")
  7. if err != nil {
  8. // HandleError(err)
  9. }

Note:

  • You cannot delete a bucket if it has objects in it. Delete all the objects in the bucket before deleting it.
  • If the bucket to be deleted has unfinished upload requests, you must cancel the requests first using the Bucket.ListMultipartUploads and Bucket.AbortMultipartUpload operations. For usage, see OSS API documentation overview.

Check whether a bucket exists

Use the Client.IsBucketExist operation to check whether a bucket of the current user exists:

  1. import "github.com/aliyun/aliyun-oss-go-sdk/oss"
  2. client, err := oss.New("Endpoint", "AccessKeyId", "AccessKeySecret")
  3. if err != nil {
  4. // HandleError(err)
  5. }
  6. isExist, err := client.IsBucketExist("my-bucket")
  7. if err != nil {
  8. // HandleError(err)
  9. }

Bucket access permissions

You can set the ACL policy of a bucket to allow or prohibit anonymous reads/writes to the bucket. For more information on ACL, see Access control.

Note: For example code of bucket ACL, see sample/bucket_acl.go.

View the ACL of a bucket

You can use Client.GetBucketACL to view the ACL policy of a bucket:

  1. import "fmt"
  2. import "github.com/aliyun/aliyun-oss-go-sdk/oss"
  3. client, err := oss.New("Endpoint", "AccessKeyId", "AccessKeySecret")
  4. if err != nil {
  5. // HandleError(err)
  6. }
  7. aclRes, err := client.GetBucketACL("my-bucket")
  8. if err != nil {
  9. // HandleError(err)
  10. }
  11. fmt.Println("Bucket ACL:", aclRes.ACL)

Set the ACL policy of a bucket

You can use Client.SetBucketACL to configure the ACL policy of a bucket:

  1. import "github.com/aliyun/aliyun-oss-go-sdk/oss"
  2. client, err := oss.New("Endpoint", "AccessKeyId", "AccessKeySecret")
  3. if err != nil {
  4. // HandleError(err)
  5. }
  6. err = client.SetBucketACL("my-bucket", oss.ACLPublicRead)
  7. if err != nil {
  8. // HandleError(err)
  9. }

Note: A bucket has three types of ACLs: private, public-read, and public-read-write. The three ACLs correspond to three constants (ACLPrivate, ACLPublicRead, and ACLPublicReadWrite) of Go SDK respectively.

Thank you! We've received your feedback.