All Products
Search
Document Center

Object Storage Service:Create buckets using OSS SDK for PHP 2.0

Last Updated:Sep 21, 2026

A bucket is a container for storing objects. You can use Object Storage Service (OSS) SDK for PHP to create buckets.

Notes

  • The sample code uses the region ID cn-hangzhou of the China (Hangzhou) region and the public endpoint by default. To access resources in a bucket from other Alibaba Cloud services in the same region, use an internal endpoint. For more information about supported regions and endpoints, see OSS regions and endpoints.

  • The oss:PutBucket permission is required to create buckets. For details, see Grant a custom policy.

  • Starting at 10:00:00 (UTC+8) on October 13, 2025, OSS gradually enables Block public access by default for buckets that are created by calling API operations or by using OSS SDKs or ossutil in all regions. For the time when the change takes effect in each region, see Announcement. After this feature is enabled, you cannot grant public access permissions, including the public-read or public-read-write ACL and bucket policies that allow public access. If your business requires public access, you can disable block public access after the bucket is created.

Sample code

The following sample code shows how to create a bucket.

<?php

// Automaticically load objects and dependency libraries.
require_once __DIR__ . '/../vendor/autoload.php'; 

use AlibabaCloud\Oss\V2 as Oss;

// Specify command line parameters.
$optsdesc = [
    "region" => ['help' => 'The region in which the bucket is located.', 'required' => True], // The region parameter is required. Example: oss-cn-hangzhou.
    "endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // The endpoint parameter is optional.
    "bucket" => ['help' => 'The name of the bucket', 'required' => True], // The name of the bucket is required.
];

// Generate a list of long options for parsing command line parameters.
$longopts = \array_map(function ($key) {
    return "$key:"; // The colon (:) following each parameter indicates that the parameter is required.
}, array_keys($optsdesc));

// Parse command line parameters.
$options = getopt("", $longopts); 

// Check whether the required parameters have been configured.
foreach ($optsdesc as $key => $value) {
    if ($value['required'] === True && empty($options[$key])) {
        $help = $value['help'];
        echo "Error: the following arguments are required: --$key, $help"; // Specifies that the required parameters are not configured.
        exit(1); 
    }
}

// Retrieve the values of the command line parameters.
$region = $options["region"]; // Region in which the bucket is located.
$bucket = $options["bucket"]; // Name of the bucket.

// Load the credential information (AccessKeyId and AccessKeySecret) from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider(); 

// Use the default configuration of the SDK.
$cfg = Oss\Config::loadDefault(); 
$cfg->setCredentialsProvider($credentialsProvider); // Specify the credential provider.
$cfg->setRegion($region); // Specify the region.
if (isset($options["endpoint"])) {
    $cfg->setEndpoint($options["endpoint"]); // Specify the endpoint if one is provided.
}

// Create an OSSClient instance.
$client = new Oss\Client($cfg); 

// Create a request to initiate bucket creation.
$request = new Oss\Models\PutBucketRequest($bucket); 

// Call the putBucket method.
$result = $client->putBucket($request);

// Output the result.
printf(
    'status code:' . $result->statusCode . PHP_EOL . // HTTP status code.
    'request id:' . $result->requestId // Unique ID of the request.
);

References