A bucket is a container for storing objects. Files that you upload are stored as objects in buckets. This topic describes how to create a bucket.
Notes
The sample code in this topic uses the region ID
cn-hangzhou
for the China (Hangzhou) region. By default, a public endpoint is used to access resources in a bucket. If you want to access resources in the bucket from other Alibaba Cloud services in the same region, use an internal endpoint. For more information about the supported regions and endpoints, see OSS regions and endpoints.To create a bucket, you must have the
oss:PutBucket
permission. For more information, see Attach a custom policy to a RAM user.From 10:00 (UTC+8) on October 13, 2025, OSS will implement a phased adjustment across all regions to enable Block Public Access by default for new buckets created using the API, OSS SDKs, or ossutil. For details about the exact times when the adjustment will take effect in each region, see [Official Announcement] Adjustment to Public Access Blocking Configurations for Newly Created Buckets. Once Block Public Access is enabled, you cannot configure public access permissions, including public ACLs (public read and public read/write) and bucket policies that allows public access. You can disable this feature after the bucket is created if your business requires public access.
Sample code
You can use the following code to create a bucket.
<?php // Import the autoloader file to load dependency libraries. require_once __DIR__ . '/../vendor/autoload.php'; use AlibabaCloud\Oss\V2 as Oss; // Define the descriptions for command line arguments. $optsdesc = [ "region" => ['help' => 'The region in which the bucket is located.', 'required' => True], // The region is required. It specifies the region where the bucket is located, for example, oss-cn-hangzhou. "endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // The endpoint is optional. It is the domain name that other services can use to access OSS. "bucket" => ['help' => 'The name of the bucket', 'required' => True], // The bucket name is required. ]; // Generate a long options array to parse command line arguments. $longopts = \array_map(function ($key) { return "$key:"; // A colon (:) after each parameter indicates that a value is required. }, array_keys($optsdesc)); // Parse the command line arguments. $options = getopt("", $longopts); // Check if any required arguments are missing. foreach ($optsdesc as $key => $value) { if ($value['required'] === True && empty($options[$key])) { $help = $value['help']; echo "Error: the following arguments are required: --$key, $help"; // Prompt the user that a required argument is missing. exit(1); } } // Obtain the values of the command line arguments. $region = $options["region"]; // The region where the bucket is located. $bucket = $options["bucket"]; // The name of the bucket. // Load credential information (AccessKeyId and AccessKeySecret) from environment variables. $credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider(); // Use the default configurations of the SDK. $cfg = Oss\Config::loadDefault(); $cfg->setCredentialsProvider($credentialsProvider); // Set the credential provider. $cfg->setRegion($region); // Set the region. if (isset($options["endpoint"])) { $cfg->setEndpoint($options["endpoint"]); // If an endpoint is provided, set the endpoint. } // Create an OSS client instance. $client = new Oss\Client($cfg); // Create a request object for creating a bucket. $request = new Oss\Models\PutBucketRequest($bucket); // Call the putBucket method to create the bucket. $result = $client->putBucket($request); // Print the result. printf( 'status code:' . $result->statusCode . PHP_EOL . // The HTTP response status code. 'request id:' . $result->requestId // Request ID. The unique identifier of the request. );
References
For the complete sample code for creating buckets, see GitHub example.
For more information about the API operation for creating buckets, see PutBucket.