Updated on 2026-09-22 GMT+08:00

Creating a Bucket

If you have any questions during development, post them on the Issues page of GitHub. For details about parameters and usage of each API, see API Reference.

You can call ObsClient->createBucket to create a bucket. Sample code is as follows:

<?php

// Import the dependency library.
require 'vendor/autoload.php';
// Import the SDK code library during the installation with the source code.
// require 'obs-autoloader.php';
// Declare the namespace.
use Obs\ObsClient;
use Obs\ObsException;
// Create an ObsClient instance.
$obsClient = new ObsClient([ 
    // Obtain an AK/SK pair using environment variables (recommended) or import an AK/SK pair in other ways. Using hard coding may result in leakage.
    //Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
    'key' => getenv('ACCESS_KEY_ID'),
    'secret' => getenv('SECRET_ACCESS_KEY'),
    'endpoint' => 'https://your-endpoint',
]);

// Create a bucket.
try {
    $resp = $obsClient->createBucket([
        'Bucket' => 'bucketname',
        // Set the bucket ACL to public read. (If the ACL is not set, the default state is private read and write.)
        'ACL' => ObsClient::AclPublicRead,
         // Set the bucket storage class to Standard.
        'StorageClass' => ObsClient::StorageClassStandard,
        // Set the bucket location.
        'LocationConstraint' => 'bucketlocation'
   ]);
   printf("RequestId:%s\n", $resp['RequestId']);
} catch (ObsException $e) {
      // If the operation fails, obtain the exception details.
      printf("CreateBucketFailed: Status:%d, Code:%s, Message:%s\n",
        $e->getStatusCode(), $e->getExceptionCode(), $e->getExceptionMessage());
} finally {
      // Close ObsClient to release resources.
      $obsClient->close();
}
  • In this example, a bucket with a public-read ACL is created in the default region. The default storage class is Standard.
  • You can use parameter ACL to specify the bucket ACL, use StorageClass to specify the storage class of the bucket, and use LocationConstraint to specify the bucket location.
  • During bucket creation, if the endpoint you use corresponds to the default region CN North-Beijing1 (cn-north-1), specifying a region is not a must. If the endpoint you use corresponds to any other region, except the default one, you must set the region to the one the used endpoint corresponds to. To view the valid regions, see Regions and Endpoints. For example, if the endpoint used for initialization is obs.ap-southeast-1.myhuaweicloud.com, you must set Location to ap-southeast-1 when you create a bucket. Otherwise, status code 400 will be returned.