A bucket is a container that is used to store objects 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.

List all buckets that belong to an Alibaba Cloud account

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

# -*- coding: utf-8 -*-
import oss2

# The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
# 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 that belong to the current account. 
for b in oss2.BucketIterator(service):
    print(b.name)

List buckets whose names contain a specified prefix

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

# -*- coding: utf-8 -*-
import oss2

# The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
# 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 buckets whose names are alphabetically after the bucket specified by marker

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

# -*- coding: utf-8 -*-
import oss2

# The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
# 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 more information about the complete sample code that is used to list buckets, visit GitHub.
  • For the API operation that you can call to list buckets, see GetService (ListBuckets).