A bucket is a container for objects in Object Storage Service (OSS). To store data in OSS, first create a bucket using the OSS SDK for PHP V2.
Prerequisites
Before you begin, make sure that you have:
PHP 7.4 or later installed
The
alibabacloud/oss-v2package installed via ComposerAn AccessKey pair (AccessKey ID and AccessKey secret) configured as environment variables (
OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRET)The
oss:PutBucketpermission granted to your RAM user. For details, see Attach a custom policy to a RAM user
Usage notes
The sample code uses the region ID
cn-hangzhoufor the China (Hangzhou) region. For all supported regions and endpoints, see OSS regions and endpoints.To access a bucket from another Alibaba Cloud service in the same region, use an internal endpoint instead of the default public endpoint.
Starting from October 13, 2025 (10:00 UTC+8), OSS enables Block Public Access by default for new buckets created through the API, SDKs, or ossutil. This applies across all regions in a phased rollout. With Block Public Access enabled, you cannot configure public access control list (ACL) settings (public read, public read/write) or bucket policies that allow public access. To allow public access, disable this feature after the bucket is created. For the rollout schedule by region, see Adjustment to public access blocking configurations for newly created buckets.
Quick start
Create a bucket with default settings in three steps: initialize a client, build a request, and call putBucket.
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Load credentials from environment variables.
// Do not hardcode credentials in your source code.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Initialize the client.
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion('cn-hangzhou'); // Replace with your region.
$client = new Oss\Client($cfg);
// Create the bucket.
$request = new Oss\Models\PutBucketRequest('examplebucket');
$result = $client->putBucket($request);
printf('Status code: %d%sRequest ID: %s%s',
$result->statusCode, PHP_EOL,
$result->requestId, PHP_EOL
);Replace cn-hangzhou with your target region and examplebucket with a globally unique bucket name.
Complete sample code
The following example parses command-line arguments and includes error handling.
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Define command-line arguments.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True],
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False],
"bucket" => ['help' => 'The name of the bucket', 'required' => True],
];
// Parse command-line arguments.
$longopts = \array_map(function ($key) {
return "$key:";
}, array_keys($optsdesc));
$options = getopt("", $longopts);
// Validate required arguments.
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help'];
echo "Error: the following arguments are required: --$key, $help";
exit(1);
}
}
$region = $options["region"];
$bucket = $options["bucket"];
// Load AccessKey ID and AccessKey secret from environment variables.
// Do not hardcode credentials in your source code.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Configure the client.
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
$client = new Oss\Client($cfg);
// Create the bucket.
try {
$request = new Oss\Models\PutBucketRequest($bucket);
$result = $client->putBucket($request);
printf('Status code: %d%sRequest ID: %s%s',
$result->statusCode, PHP_EOL,
$result->requestId, PHP_EOL
);
} catch (\Exception $e) {
printf('Error: %s%s', $e->getMessage(), PHP_EOL);
exit(1);
}Run the script:
php create_bucket.php --region cn-hangzhou --bucket examplebucketCommand-line arguments
| Argument | Required | Description |
|---|---|---|
--region | Yes | Region where the bucket is located, for example, cn-hangzhou |
--endpoint | No | Custom endpoint for accessing OSS. If not specified, the default endpoint for the region is used |
--bucket | Yes | Globally unique bucket name |
Response fields
| Field | Description |
|---|---|
$result->statusCode | HTTP status code |
$result->requestId | Unique request ID for troubleshooting |