• Object Storage Service

obs
  1. Help Center
  2. Object Storage Service
  3. Developer Guide (PHP 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 third-party open source libraries.
require 'vendor/autoload.php';
// Import the SDK code library.
require 'obs-autoloader.php';
// Declare the namespace.
use Obs\S3\ObsClient;
// Create an instance of ObsClient.
$obsClient = new ObsClient([
       'key' => '*** Provide your Access Key ***',
       'secret' => '*** Provide your Secret Key ***',
       'endpoint' => 'https://yourdomainname'
]);

try{
       $resp = $obsClient->headBucket([
              'Bucket' => 'bucketname'
       ]);
       printf("Bucket exists");
}catch (\Obs\Common\ObsException $obsException){
       if($obsException->getStatusCode() === 404){
              printf("Bucket does not exist");
       }else{
              printf("ExceptionCode:%s\n", $obsException->getExceptionCode());       
              printf("getExceptionMessage:%s\n", $obsException->getExceptionMessage());
       }
}
NOTE:

If an exception is thrown and the returned HTTP status code is 404, the bucket does not exist.