All Products
Search
Document Center

Object Storage Service:List buckets

Last Updated:Mar 12, 2025

This topic describes how to list buckets that belong to the current Alibaba Cloud account in all regions and meet specific conditions.

Usage notes

  • The sample code in this topic uses the region ID cn-hangzhou of the China (Hangzhou) region. By default, a public endpoint is used to access resources in a bucket. If you want to access resources in the bucket by using other Alibaba Cloud services in the same region in which the bucket is located, use an internal endpoint. For more information about Object Storage Service (OSS) regions and endpoints, see Regions and endpoints.

  • To list buckets, you must have the oss:ListBuckets permission. For more information, see Attach a custom policy to a RAM user.

Examples

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

import argparse
import alibabacloud_oss_v2 as oss

# Create a command line parameter parser and describe the purpose of the script. The example describes how to list all buckets in OSS.
parser = argparse.ArgumentParser(description="list buckets sample")

# Specify the command line parameter --region, which specifies the region in which the bucket is located. This parameter is required.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Specify the command line parameter --endpoint, which specifies the endpoint that other services can use to access OSS. This parameter is optional.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')

def main():
    # Parse the parameters provided in the command line to obtain the values entered by the user.
    args = parser.parse_args()

    # Load the authentication information required to access OSS from the environment variables.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Use the default configurations of the SDK to create a configuration object and specify the credential provider.
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region

    # If a custom endpoint is provided, modify the endpoint parameter in the configuration object.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Use the preceding configuration to initialize the OSSClient instance and allow the instance to interact with OSS.
    client = oss.Client(cfg)

    # Create a paginator to allow the ListBuckets operation to list a large number of buckets.
    paginator = client.list_buckets_paginator()

    # Traverse the listed buckets.
    for page in paginator.iter_page(oss.ListBucketsRequest()):
        # Display the name, location, creation date, and resource group ID of each bucket on each page.
        for o in page.buckets:
            print(f'Bucket: {o.name}, Location: {o.location}, Created: {o.creation_date}, Resource Group ID: {o.resource_group_id}')

# Call the main function to start the processing logic when the script is directly run.
if __name__ == "__main__":
    main() # Specify the entry points in functions of the script. The control program flow starts here.

Common scenarios

List buckets whose names contain a specific prefix

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

import argparse
import alibabacloud_oss_v2 as oss

# Create a command line parameter parser and describe the purpose of the script. The example describes how to list all buckets in OSS.
parser = argparse.ArgumentParser(description="list buckets sample")

# Specify the command line parameter --region, which specifies the region in which the bucket is located. This parameter is required.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Specify the command line parameter --endpoint, which specifies the endpoint that other services can use to access OSS. This parameter is optional.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')

def main():
    # Parse the parameters provided in the command line to obtain the values entered by the user.
    args = parser.parse_args()

    # Load the authentication information required to access OSS from the environment variables.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Use the default configurations of the SDK to create a configuration object and specify the credential provider.
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region

    # If a custom endpoint is provided, modify the endpoint parameter in the configuration object.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Use the preceding configuration to initialize the OSSClient instance and allow the instance to interact with OSS.
    client = oss.Client(cfg)

    # Create a paginator to allow the ListBuckets operation to list a large number of buckets.
    paginator = client.list_buckets_paginator()

    # Traverse the listed buckets.
    for page in paginator.iter_page(oss.ListBucketsRequest(
        prefix='example', # Set the prefix parameter to example. This way, only buckets whose names contain the example prefix are listed.
        ),
    ):
        # Display the name, location, creation date, and resource group ID of each bucket on each page.
        for o in page.buckets:
            print(f'Bucket: {o.name}, Location: {o.location}, Created: {o.creation_date}, Resource Group ID: {o.resource_group_id}')

# Call the main function to start the processing logic when the script is directly run.
if __name__ == "__main__":
    main() # Specify the entry points in functions of the script. The control program flow starts here.

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

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

import argparse
import alibabacloud_oss_v2 as oss

# Create a command line parameter parser and describe the purpose of the script. The example describes how to list all buckets in OSS.
parser = argparse.ArgumentParser(description="list buckets sample")

# Specify the command line parameter --region, which specifies the region in which the bucket is located. This parameter is required.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Specify the command line parameter --endpoint, which specifies the endpoint that other services can use to access OSS. This parameter is optional.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')

def main():
    # Parse the parameters provided in the command line to obtain the values entered by the user.
    args = parser.parse_args()

    # Load the authentication information required to access OSS from the environment variables.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Use the default configurations of the SDK to create a configuration object and specify the credential provider.
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region

    # If a custom endpoint is provided, modify the endpoint parameter in the configuration object.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Use the preceding configuration to initialize the OSSClient instance and allow the instance to interact with OSS.
    client = oss.Client(cfg)

    # Create a paginator to allow the ListBuckets operation to list a large number of buckets.
    paginator = client.list_buckets_paginator()

    # Traverse the listed buckets.
    for page in paginator.iter_page(oss.ListBucketsRequest(
        marker="example-bucket", # Set the marker parameter to example-bucket. This way, the buckets whose names are alphabetically after the example-bucket bucket are listed.
        ),
    ):
        # Display the name, location, creation date, and resource group ID of each bucket on each page.
        for o in page.buckets:
            print(f'Bucket: {o.name}, Location: {o.location}, Created: {o.creation_date}, Resource Group ID: {o.resource_group_id}')

# Call the main function to start the processing logic when the script is directly run.
if __name__ == "__main__":
    main() # Specify the entry points in functions of the script. The control program flow starts here.

List a specific number of buckets

The following sample code provides an example on how to list buckets in all regions within the current Alibaba Cloud account and specify the maximum number of buckets that can be listed per page:

import argparse
import alibabacloud_oss_v2 as oss

# Create a command line parameter parser and describe the purpose of the script. The example describes how to list all buckets in OSS.
parser = argparse.ArgumentParser(description="list buckets sample")

# Specify the command line parameter --region, which specifies the region in which the bucket is located. This parameter is required.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Specify the command line parameter --endpoint, which specifies the endpoint that other services can use to access OSS. This parameter is optional.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')

def main():
    # Parse the parameters provided in the command line to obtain the values entered by the user.
    args = parser.parse_args()

    # Load the authentication information required to access OSS from the environment variables.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Use the default configurations of the SDK to create a configuration object and specify the credential provider.
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region

    # If a custom endpoint is provided, modify the endpoint parameter in the configuration object.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Use the preceding configuration to initialize the OSSClient instance and allow the instance to interact with OSS.
    client = oss.Client(cfg)

    # Create a paginator for the ListBuckets operation to list a large number of buckets.
    paginator = client.list_buckets_paginator()

    # Traverse the listed buckets. Each page contains a specific number of buckets.
    for page in paginator.iter_page(oss.ListBucketsRequest(
        max_keys=10, # Specify that up to 10 buckets can be returned per page.
        ),
    ):
        # Display the name, location, creation date, and resource group ID of each bucket on each page.
        for o in page.buckets:
            print(f'Bucket: {o.name}, Location: {o.location}, Created: {o.creation_date}, Resource Group ID: {o.resource_group_id}')

# Call the main function to start the processing logic when the script is directly run.
if __name__ == "__main__":
    main() # Specify the entry points in functions of the script. The control program flow starts here.

References

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