All Products
Search
Document Center

Object Storage Service:List buckets (Ruby SDK)

Last Updated:Mar 20, 2026

Use the OSS Ruby SDK to list buckets in your Alibaba Cloud account. Buckets are returned in alphabetical order. You can retrieve all buckets or filter by prefix.

Prerequisites

Before you begin, make sure that you have:

  • Set the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables with valid credentials

  • An OSS endpoint URL for your region (for example, https://oss-cn-hangzhou.aliyuncs.com for China (Hangzhou))

List all buckets

The following example lists all buckets across all regions in your account.

require 'aliyun/oss'

client = Aliyun::OSS::Client.new(
  # Replace with your actual endpoint.
  endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
  # Read credentials from environment variables.
  # Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this example.
  access_key_id: ENV['OSS_ACCESS_KEY_ID'],
  access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)

buckets = client.list_buckets
buckets.each { |b| puts b.name }

List buckets with a specified prefix

To filter buckets by name, pass the :prefix option to list_buckets. The following example lists buckets whose names start with example.

require 'aliyun/oss'

client = Aliyun::OSS::Client.new(
  # Replace with your actual endpoint.
  endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
  # Read credentials from environment variables.
  # Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this example.
  access_key_id: ENV['OSS_ACCESS_KEY_ID'],
  access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)

buckets = client.list_buckets(:prefix => 'example')
buckets.each { |b| puts b.name }

References

For more information about the ListBuckets API operation, see ListBuckets (GetService).