All Products
Search
Document Center

Object Storage Service:List buckets

Last Updated:Oct 17, 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.

List buckets

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

require 'aliyun/oss'

client = Aliyun::OSS::Client.new(
  # In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
  endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
  # 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. 
  access_key_id: ENV['OSS_ACCESS_KEY_ID'],
  access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)
# List all buckets in all regions within the current account. 
buckets = client.list_buckets
buckets.each { |b| puts b.name }

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

require 'aliyun/oss'

client = Aliyun::OSS::Client.new(
  # In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
  endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
  # 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. 
  access_key_id: ENV['OSS_ACCESS_KEY_ID'],
  access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)
# List buckets whose names contain the example prefix in all regions within the current Alibaba Cloud account. 
buckets = client.list_buckets(:prefix => 'example')
buckets.each { |b| puts b.name }

References

For more information about the API operation that you can call to list buckets, see ListBuckets (GetService).