All Products
Search
Document Center

Object Storage Service:Determine whether a bucket exists

Last Updated:Oct 19, 2023

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

Examples

The following code provides an example on how to determine whether a bucket named examplebucket exists:

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']
)
# Determine whether the examplebucket bucket exists. 
puts client.bucket_exists?('examplebucket')
# If true is returned, the examplebucket bucket exists. If false is returned, the examplebucket bucket does not exist.