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

For the complete code used to list buckets, visit GitHub.

For more information about how to list buckets, see GetBucket (ListObject).

List all buckets

Buckets are listed in alphabetical order. You can list all buckets or only buckets that meet specified conditions.

The following code provides an example on how to list all buckets:

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)
    }

    // List all buckets.
    marker := ""
    for {
        lsRes, err := client.ListBuckets(oss.Marker(marker))
        if err ! = nil {
            fmt.Println("Error:", err)
            os.Exit(-1)
        }

        // By default, 100 buckets are listed each time. 
        for _, bucket := range lsRes.Buckets {
            fmt.Println("Bucket: ", bucket.Name)
        }

        if lsRes.IsTruncated {
            marker = lsRes.NextMarker
        } else {
            break
        }
    }
}
			

List buckets whose names contain a specified prefix

The following code provides an example on how to list buckets whose names contain a specified prefix:

// List the buckets whose names contain a specified prefix.
    lsRes, err = client.ListBuckets(oss.Prefix("<yourBucketPrefix>"))
    if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    // Obtain the list of buckets.
    fmt.Println("Buckets with prefix: ", lsRes.Buckets)
    for _, bucket := range lsRes.Buckets {
        fmt.Println("Bucket with prefix: ", bucket.Name)
    }

List buckets that follow a specified marker

The marker parameter specifies the name of the bucket after which the list begins. The following code provides an example on how to list buckets that follow a specified marker:

    // List the buckets that follow a specified marker.
    lsRes, err = client.ListBuckets(oss.Marker("<yourBucketMarker>"))
    if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Obtain the list of buckets.
    fmt.Println("My buckets with marker :", lsRes.Buckets)
    for _, bucket := range lsRes.Buckets {
        fmt.Println("Bucket with marker: ", bucket.Name)
    }
			

List a specified number of buckets

The following code provides an example on how to list a specified number (MaxKeys) of buckets:

    // Set the number of buckets to list each time to 500. The default value is 100. The maximum value is 1000.
    lsRes, err = client.ListBuckets(oss.MaxKeys(500))
    if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Obtain the list of buckets. 
    fmt.Println("My buckets max num:", lsRes.Buckets)
    for _, bucket := range lsRes.Buckets {
        fmt.Println("Bucket with maxKeys: ", bucket.Name)
    }