All Products
Search
Document Center

Object Storage Service:List buckets

Last Updated:Aug 31, 2023

A bucket is a container for objects stored in Object Storage Service (OSS). All objects in OSS are contained in buckets. Bucket names are listed in alphabetical order. You can list buckets that belong to the current Alibaba Cloud account in all regions and meet the specific conditions.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS by using other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about the regions and endpoints supported by OSS, see Regions and endpoints.

  • In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Initialization.

  • To list buckets, you must have the oss:ListBuckets permission. For more information, see Common examples of RAM policies.

List all buckets within an Alibaba Cloud account

The following sample code provides an example on how to list buckets in all regions within the current Alibaba Cloud account:

package main

import (
	"fmt"
	"os"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// Create an OSSClient instance. 
	// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// List the buckets in all regions within the current Alibaba Cloud account. 
	marker := ""
	for {
		lsRes, err := client.ListBuckets(oss.Marker(marker))
		if err != nil {
			fmt.Println("Error:", err)
			os.Exit(-1)
		}

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

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

List the buckets whose names contain a specified prefix

The following sample code provides an example on how to list the buckets whose names contain the example prefix in all regions within the current Alibaba Cloud account:

package main

import (
	"fmt"
	"os"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// Create an OSSClient instance. 
	// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// List all buckets whose names contain the example prefix within the current Alibaba Cloud account. 
	lsRes, err := client.ListBuckets(oss.Prefix("example"))
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	// Display the buckets. 
	fmt.Println("Buckets with prefix: ", lsRes.Buckets)
	for _, bucket := range lsRes.Buckets {
		fmt.Println("Bucket with prefix: ", bucket.Name)
	}
}

List the buckets whose names are alphabetically after the bucket specified by marker

The following sample code provides an example on how to list the buckets whose names are alphabetically after the bucket named examplebucket in all regions within the current Alibaba Cloud account:

package main

import (
	"fmt"
	"os"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// Create an OSSClient instance. 
	// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	// List all buckets whose names are alphabetically after examplebucket within the current Alibaba Cloud account. 
	lsRes, err := client.ListBuckets(oss.Marker("examplebucket"))
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// Display the 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 sample code provides an example on how to list a maximum of 500 buckets in all regions within the current Alibaba Cloud account:

package main

import (
	"fmt"
	"os"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// Create an OSSClient instance. 
	// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	// List all buckets within the current Alibaba Cloud account. Set the maximum number of buckets that can be listed to 500. Default value: 100. Maximum value: 1000. 
	lsRes, err := client.ListBuckets(oss.MaxKeys(500))
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

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

References

  • For the complete sample code that is used to list buckets, visit GitHub.

  • For the API operation that you can call to list buckets, see ListBuckets (GetService).