All Products
Search
Document Center

Object Storage Service:Check whether a bucket exists (Ruby SDK)

Last Updated:Nov 29, 2025

A bucket is a container used to store objects in Object Storage Service (OSS). This topic describes how to determine whether a bucket exists.

Sample code

The following code shows how to check whether the bucket named examplebucket exists:

require 'aliyun/oss'

client = Aliyun::OSS::Client.new(
  # The China (Hangzhou) region is used as an example for the endpoint. Specify the endpoint based on your region.
  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 set.
  access_key_id: ENV['OSS_ACCESS_KEY_ID'],
  access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)
# Check whether the bucket named examplebucket exists.
puts client.bucket_exists?('examplebucket')
# If true is returned, the specified bucket exists. If false is returned, the specified bucket does not exist.