• Object Storage Service

  1. Help Center
  2. Object Storage Service
  3. Developer Guide (Python SDK)
  4. Bucket Management
  5. Identifying Whether a Bucket Exists

Identifying Whether a Bucket Exists

You can call ObsClient.headBucket to identify whether a bucket exists. Sample code is as follows:

# Import the module.
from com.obs.client.obs_client import ObsClient

# Create an instance of ObsClient.
obsClient = ObsClient(
    access_key_id='*** Provide your Access Key ***',    
    secret_access_key='*** Provide your Secret Key ***',    

resp = obsClient.headBucket('bucketname')
if resp.status < 300:    
    # The bucket exists.    
    print('Bucket exists')
elif resp.status == 404:    
    # The bucket does not exist.
    print('Bucket does not exist') 

If the returned HTTP status code is 404, the bucket does not exist.