• Object Storage Service

obs
  1. Help Center
  2. Object Storage Service
  3. Developer Guide (Node.js 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 OBS library.
var ObsClient = require('./lib/obs');

// Create an instance of ObsClient.
var obsClient = new ObsClient({
    access_key_id: '*** Provide your Access Key ***',       
    secret_access_key: '*** Provide your Secret Key ***',       
    server : 'yourdomainname'
});

obsClient.headBucket({
       Bucket : 'bucketname'
}, (err, result) => {
       if(err){
              console.error('Error-->' + err);
       }else{
              if(result.CommonMsg.Status < 300){
                     console.log('Bucket exists');
              }else if(result.CommonMsg.Status === 404){
                     console.log('Bucket does not exist');
              }
       }
});
NOTE:

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