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.
List buckets
The following code shows how to list all buckets across all regions in your account.
require 'aliyun/oss'
client = Aliyun::OSS::Client.new(
# The China (Hangzhou) endpoint is used as an example. Replace it with the actual endpoint.
endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
# Obtain access credentials from environment variables. Before running this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
access_key_id: ENV['OSS_ACCESS_KEY_ID'],
access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)
# List all buckets across all regions in your account.
buckets = client.list_buckets
buckets.each { |b| puts b.name }List buckets with a specified prefix
The following code shows how to list buckets with the prefix "example" across all regions in your account.
require 'aliyun/oss'
client = Aliyun::OSS::Client.new(
# The China (Hangzhou) endpoint is used as an example. Replace it with the actual endpoint.
endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
# Obtain access credentials from environment variables. Before running this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
access_key_id: ENV['OSS_ACCESS_KEY_ID'],
access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)
# List buckets with a specified prefix across all regions in your account.
buckets = client.list_buckets(:prefix => 'example')
buckets.each { |b| puts b.name }References
For more information about the ListBuckets API operation, see ListBuckets (GetService).