All Products
Search
Document Center

Object Storage Service:List buckets

Last Updated:Sep 06, 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:

Note

You cannot run the following code to list buckets in a specific region. For example, if the region of the bucket you specified in the code is China (Hangzhou), the buckets in all regions within the current Alibaba Cloud account are listed.

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# 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. 
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# 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. 
service = oss2.Service(auth, 'https://oss-cn-hangzhou.aliyuncs.com')

# List all buckets in all regions within the current account. 
for b in oss2.BucketIterator(service):
    print(b.name)

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:

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# 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. 
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# 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. 
service = oss2.Service(auth, 'https://oss-cn-hangzhou.aliyuncs.com')

# List all buckets whose names contain the example prefix under the current account. 
for b in oss2.BucketIterator(service, prefix='example'):
    print(b.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:

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# 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. 
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# 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. 
service = oss2.Service(auth, 'https://oss-cn-hangzhou.aliyuncs.com')

# List all buckets whose names are alphabetically after examplebucket under the current account. The bucket named examplebucket is not listed. 
for b in oss2.BucketIterator(service, marker='examplebucket'):
    print(b.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).