Use ListBuckets to query all buckets in your account, with optional filters for prefix, marker, page size, and resource group. For a complete runnable project, see the GitHub example.
Prerequisites
Before you begin, make sure you have:
The
oss:ListBucketspermission. For details, see Grant custom permissions to a RAM user.Access credentials configured as environment variables. For details, see Configure access credentials.
The examples on this page use the cn-hangzhou region and connect via the public endpoint. To access OSS from another Alibaba Cloud service in the same region, use the internal endpoint instead. For endpoint mappings, see OSS regions and endpoints.Choose an API
Go SDK V2 provides two ways to call ListBuckets:
| API | When to use |
|---|---|
Paginator (NewListBucketsPaginator) | Iterating over all results automatically. The paginator handles pagination-token tracking so you only write the result-processing logic. |
Direct call (ListBuckets) | Fetching a single page, or when you need fine-grained control over each request. |
All examples below share the same client setup:
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
client := oss.NewClient(cfg)Method definitions
Examples
Common scenarios
All scenario examples share the same client setup as above. Only the ListBucketsRequest fields differ.
References
Complete runnable example: list_buckets.go on GitHub
Paginator API reference: NewListBucketsPaginator
Direct call API reference: ListBuckets
Paginator design guide: Developer guide