All Products
Search
Document Center

Object Storage Service:List buckets using OSS SDK for Python 1.0

Last Updated:Mar 20, 2026

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:

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 OSSClient instance 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

ParameterTypeDescription
serviceoss2.ServiceRequired. The initialized Service client.
prefixstrOptional. Return only buckets whose names start with this value.
markerstrOptional. 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:

AttributeDescription
b.nameThe 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.

Note

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