All Products
Search
Document Center

Object Storage Service:List buckets

Last Updated:Nov 13, 2024

A bucket is a container for objects stored in Object Storage Service (OSS). All objects in OSS are contained in buckets. Buckets are listed in alphabetical order. You can list buckets that belong to the current Alibaba Cloud account in all regions and meet specific conditions.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions, endpoints and open ports.

  • 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 Attach a custom policy to a RAM user.

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.ProviderAuthV4(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. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"

# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

service = oss2.Service(auth, endpoint, region=region)

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

List buckets whose names contain a specified 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:

# -*- 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.ProviderAuthV4(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. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"

# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

service = oss2.Service(auth, endpoint, region=region)

# List all buckets whose names contain the example prefix within 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 sample code provides an example on how to list 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.ProviderAuthV4(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. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"

# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

service = oss2.Service(auth, endpoint, region=region)

# 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 more information about the API operation that you can call to list buckets, see ListBuckets (GetService).