This topic describes how to query the storage usage of a specific Object Storage Service (OSS) bucket and the number and storage usage of objects of different storage classes in the bucket.
Considerations
In this topic, the public endpoint of the China (Hangzhou) region is used. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For details about supported regions and endpoints, see Regions and endpoints.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Create an OSSClient instance.
To retrieve the storage capacity of a bucket, you must have the
oss:GetBucketStatpermission. For more information, see Grant custom permissions to a RAM user.
Example code
The following sample code provides an example on how to query the storage usage of a bucket named examplebucket and the number and storage usage of objects of different storage classes in the bucket.
PHP SDK 2.6.0 or later is required to return all the properties shown in the following code example.
<?php
if (is_file(__DIR__ . '/../autoload.php')) {
require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
require_once __DIR__ . '/../vendor/autoload.php';
}
use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\Core\OssException;
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
$provider = new EnvironmentVariableCredentialsProvider();
// The endpoint of the China (Hangzhou) region is used as an example. Specify the actual endpoint.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the bucket name.
$bucket= "examplebucket";
try {
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
"region"=> "cn-hangzhou"
);
$ossClient = new OssClient($config);
$stat = $ossClient->getBucketStat($bucket);
} catch (OssException $e) {
printf($e->getMessage() . "\n");
return;
}
// Get the total storage capacity of the bucket in bytes.
printf("Bucket ".$bucket." current storage is:".$stat->getStorage().PHP_EOL);
// Get the total number of objects in the bucket.
printf("Bucket ".$bucket." object count is:".$stat->getObjectCount().PHP_EOL);
// Get the number of multipart upload tasks that are initiated but not completed or aborted in the bucket.
printf("Bucket ".$bucket." multipart upload count is:".$stat->getMultipartUploadCount().PHP_EOL);
// Get the number of LiveChannels in the bucket.
printf("Bucket ".$bucket." live channel count is:".$stat->getLiveChannelCount().PHP_EOL);
// The time when the storage information was obtained in this call. The value is a UNIX timestamp. Unit: seconds.
printf("Bucket ".$bucket." last modified time is:".$stat->getLastModifiedTime().PHP_EOL);
// Get the storage capacity of Standard objects in bytes.
printf("Bucket ".$bucket." standard storage is:".$stat->getStandardStorage().PHP_EOL);
// Get the number of Standard objects.
printf("Bucket ".$bucket." standard object count is:".$stat->getStandardObjectCount().PHP_EOL);
// Get the billable storage capacity of Infrequent Access objects in bytes.
printf("Bucket ".$bucket." infrequent access storage is:".$stat->getInfrequentAccessStorage().PHP_EOL);
// Get the actual storage capacity of Infrequent Access objects in bytes.
printf("Bucket ".$bucket." infrequent access real storage is:".$stat->getInfrequentAccessRealStorage().PHP_EOL);
// Get the number of Infrequent Access objects.
printf("Bucket ".$bucket." infrequent access object count is:".$stat->getInfrequentAccessObjectCount().PHP_EOL);
// Get the billable storage capacity of Archive Storage objects in bytes.
printf("Bucket ".$bucket." archive storage is:".$stat->getArchiveStorage().PHP_EOL);
// Get the actual storage capacity of Archive Storage objects in bytes.
printf("Bucket ".$bucket." archive real storage is:".$stat->getArchiveRealStorage().PHP_EOL);
// Get the number of Archive Storage objects.
printf("Bucket ".$bucket." archive object count is:".$stat->getArchiveObjectCount().PHP_EOL);
// Get the billable storage capacity of Cold Archive objects in bytes.
printf("Bucket ".$bucket." cold archive storage is:".$stat->getColdArchiveStorage().PHP_EOL);
// Get the actual storage capacity of Cold Archive objects in bytes.
printf("Bucket ".$bucket." cold archive real storage is:".$stat->getColdArchiveRealStorage().PHP_EOL);
// Get the number of Cold Archive objects.
printf("Bucket ".$bucket." cold archive object count is:".$stat->getColdArchiveObjectCount().PHP_EOL);References
For more information about the API operation that you can call to query the storage usage of a specific bucket and the number and storage usage of objects of different storage classes in the bucket, see GetBucketStat.