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.
Usage notes
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions, endpoints and open ports.
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 query the storage capacity of a bucket, you must have the
oss:GetBucketStat
permission. For more information, see Attach a custom policy to a RAM user.
Examples
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.
Only OSS SDK for PHP 2.6.0 and later support all attributes in the sample code.
<?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 configured.
$provider = new EnvironmentVariableCredentialsProvider();
// In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the name of the bucket.
$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;
}
// Query the total storage capacity of the bucket. Unit: bytes.
printf("Bucket ".$bucket." current storage is:".$stat->getStorage().PHP_EOL);
// Query the total number of objects that are stored in the bucket.
printf("Bucket ".$bucket." object count is:".$stat->getObjectCount().PHP_EOL);
// Query the number of multipart upload tasks that are initiated but are not completed or canceled.
printf("Bucket ".$bucket." multipart upload count is:".$stat->getMultipartUploadCount().PHP_EOL);
// Query the number of LiveChannels in the bucket.
printf("Bucket ".$bucket." live channel count is:".$stat->getLiveChannelCount().PHP_EOL);
// Query the time when the obtained information was last modified. The value is a UNIX timestamp. Unit: seconds.
printf("Bucket ".$bucket." last modified time is:".$stat->getLastModifiedTime().PHP_EOL);
// Query the storage capacity of Standard objects in the bucket. Unit: bytes.
printf("Bucket ".$bucket." standard storage is:".$stat->getStandardStorage().PHP_EOL);
// Query the number of Standard objects in the bucket.
printf("Bucket ".$bucket." standard object count is:".$stat->getStandardObjectCount().PHP_EOL);
// Query the billed storage capacity of Infrequent Access (IA) objects in the bucket. Unit: bytes.
printf("Bucket ".$bucket." infrequent access storage is:".$stat->getInfrequentAccessStorage().PHP_EOL);
// Query the actual storage capacity of IA objects in the bucket. Unit: bytes.
printf("Bucket ".$bucket." infrequent access real storage is:".$stat->getInfrequentAccessRealStorage().PHP_EOL);
// Query the number of IA objects in the bucket.
printf("Bucket ".$bucket." infrequent access object count is:".$stat->getInfrequentAccessObjectCount().PHP_EOL);
// Query the billed storage capacity of Archive objects in the bucket. Unit: bytes.
printf("Bucket ".$bucket." archive storage is:".$stat->getArchiveStorage().PHP_EOL);
// Query the actual storage capacity of Archive objects in the bucket. Unit: bytes.
printf("Bucket ".$bucket." archive real storage is:".$stat->getArchiveRealStorage().PHP_EOL);
// Query the number of Archive objects in the bucket.
printf("Bucket ".$bucket." archive object count is:".$stat->getArchiveObjectCount().PHP_EOL);
// Query the billed storage capacity of Cold Archive objects in the bucket. Unit: bytes.
printf("Bucket ".$bucket." cold archive storage is:".$stat->getColdArchiveStorage().PHP_EOL);
// Query the actual storage capacity of Cold Archive objects in the bucket. Unit: bytes.
printf("Bucket ".$bucket." cold archive real storage is:".$stat->getColdArchiveRealStorage().PHP_EOL);
// Query the number of Cold Archive objects in the bucket.
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.