Use BucketIterator to list all buckets under your Alibaba Cloud account. Results are returned in alphabetical order by bucket name. You can filter results by prefix or start position (marker).
Prerequisites
Before you begin, ensure that you have:
The
oss:ListBucketspermission. For details, see Attach a custom policy to a RAM userThe
OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETenvironment variables set. For details, see Configure access credentials
Usage notes
The examples use the public endpoint for the China (Hangzhou) region (
https://oss-cn-hangzhou.aliyuncs.com). To access OSS from another Alibaba Cloud service in the same region, use an internal endpoint instead. For a full list of endpoints, see Regions and endpoints.The
OSSClientinstance in these examples is initialized with an OSS endpoint. To initialize with a custom domain name or Security Token Service (STS), see Initialization.
Set up the client
All examples in this topic share the same client setup. Initialize the Service client once and reuse it across operations:
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Load access credentials from environment variables.
# Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this code.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint for the region. This example uses China (Hangzhou).
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region ID. Required for V4 signatures.
region = "cn-hangzhou"
service = oss2.Service(auth, endpoint, region=region)BucketIterator parameters
| Parameter | Type | Description |
|---|---|---|
service | oss2.Service | Required. The initialized Service client. |
prefix | str | Optional. Return only buckets whose names start with this value. |
marker | str | Optional. Return only buckets whose names are alphabetically after this value. The specified bucket is not included in the results. |
Each bucket object returned by the iterator exposes the following attribute:
| Attribute | Description |
|---|---|
b.name | The bucket name. |
List all buckets
BucketIterator returns all buckets under your Alibaba Cloud account across every region. The endpoint and region in the client configuration do not filter results by region.
This operation lists buckets across all regions regardless of which region the client is initialized for. Region-based filtering is not supported.
for b in oss2.BucketIterator(service):
print(b.name)List buckets by prefix
Pass a prefix string to return only buckets whose names start with that value:
for b in oss2.BucketIterator(service, prefix='example'):
print(b.name)Replace 'example' with your target prefix.
List buckets after a marker
Pass a marker string to return only buckets whose names are alphabetically after the specified value. The marker itself is not included in the results:
for b in oss2.BucketIterator(service, marker='examplebucket'):
print(b.name)Replace 'examplebucket' with the bucket name to use as the starting point.
What's next
For the complete sample code, see GitHub examples.
For the underlying API operation, see ListBuckets (GetService).