Use the OSS PHP SDK V2 to call GetBucketStat and retrieve the storage usage and object count of a bucket, broken down by storage class.
Prerequisites
Before you begin, ensure that you have:
An Alibaba Cloud account with access to Object Storage Service (OSS)
An OSS bucket in the target region
AccessKey ID and AccessKey secret stored as environment variables
OSS PHP SDK V2 installed via Composer
Usage notes
The sample code uses the China (Hangzhou) region (
cn-hangzhou) as the example region. For a full list of region IDs and their endpoints, see Regions and endpoints.By default, the sample uses a public endpoint. To access OSS from another Alibaba Cloud service in the same region, pass the internal endpoint using
--endpoint.
How it works
The sample follows three steps:
Initialize the client — Load credentials from environment variables and create an
Oss\Clientinstance configured for your region.Call `getBucketStat` — Pass the bucket name in a
GetBucketStatRequestand call$client->getBucketStat($request).Process the result — The result object contains the bucket statistics, including the number and storage usage of objects of different storage classes.
Query storage capacity
The following sample code queries the storage usage of a bucket:
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Define and parse command-line parameters.
$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],
];
$longopts = \array_map(function ($key) {
return "$key:";
}, array_keys($optsdesc));
$options = getopt("", $longopts);
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 credentials from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Configure and create 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);
// Query bucket statistics.
$request = new Oss\Models\GetBucketStatRequest($bucket);
$result = $client->getBucketStat($request);
// Print the result.
printf(
'HTTP status code: ' . $result->statusCode . PHP_EOL .
'Request ID: ' . $result->requestId . PHP_EOL .
'Result: ' . var_export($result, true)
);Run the sample with your bucket and region:
php GetBucketStat.php --region cn-hangzhou --bucket <your-bucket-name>Replace the following placeholders:
| Placeholder | Description | Example |
|---|---|---|
<your-bucket-name> | The name of your OSS bucket | my-bucket |
What's next
For the complete sample, see GetBucketStat.php on GitHub.
For the full list of fields returned by this operation, see the GetBucketStat API reference.