Use get_bucket_stat to retrieve storage statistics for a bucket: total capacity, object counts, and per-storage-class breakdowns.
Prerequisites
Before you begin, ensure that you have:
An OSS bucket in the target region
The
ALIBABA_CLOUD_ACCESS_KEY_IDandALIBABA_CLOUD_ACCESS_KEY_SECRETenvironment variables set with valid credentialsOSS SDK for Python 2.0 installed (
pip install alibabacloud-oss-v2)
Usage notes
The sample code uses region ID
cn-hangzhou(China (Hangzhou)). Replace it with your bucket's region.By default, the SDK connects via the public endpoint. To access OSS from another Alibaba Cloud service in the same region, set the
endpointparameter to the internal endpoint. For supported regions and endpoints, see Regions and endpoints.
Method definition
get_bucket_stat(request: GetBucketStatRequest, **kwargs) → GetBucketStatResultRequest parameters
| Parameter | Type | Description |
|---|---|---|
request | GetBucketStatRequest | The request parameters. See GetBucketStatRequest. |
Response type
| Type | Description |
|---|---|
GetBucketStatResult | The response parameters. See GetBucketStatResult. |
For the complete method definition, see get_bucket_stat.
Sample code
The following example retrieves statistics for a bucket specified via command-line arguments. Credentials are loaded from environment variables.
import argparse
import alibabacloud_oss_v2 as oss
parser = argparse.ArgumentParser(description="Get statistical information about a specified OSS bucket.")
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket to get statistics for.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS.')
def main():
args = parser.parse_args()
# Load credentials from environment variables
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = args.region
if args.endpoint is not None:
cfg.endpoint = args.endpoint
client = oss.Client(cfg)
request = oss.GetBucketStatRequest(bucket=args.bucket)
result = client.get_bucket_stat(request)
print(f'status code: {result.status_code},'
f' request id: {result.request_id},'
f' storage: {result.storage},'
f' object count: {result.object_count},'
f' multi part upload count: {result.multi_part_upload_count},'
f' live channel count: {result.live_channel_count},'
f' last modified time: {result.last_modified_time},'
f' standard storage: {result.standard_storage},'
f' standard object count: {result.standard_object_count},'
f' infrequent access storage: {result.infrequent_access_storage},'
f' infrequent access real storage: {result.infrequent_access_real_storage},'
f' infrequent access object count: {result.infrequent_access_object_count},'
f' archive storage: {result.archive_storage},'
f' archive real storage: {result.archive_real_storage},'
f' archive object count: {result.archive_object_count},'
f' cold archive storage: {result.cold_archive_storage},'
f' cold archive real storage: {result.cold_archive_real_storage},'
f' cold archive object count: {result.cold_archive_object_count},'
f' deep cold archive storage: {result.deep_cold_archive_storage},'
f' deep cold archive real storage: {result.deep_cold_archive_real_storage},'
f' deep cold archive object count: {result.deep_cold_archive_object_count},'
f' delete marker count: {result.delete_marker_count},'
)
if __name__ == "__main__":
main()For the complete sample, see get_bucket_stat.py.
Response fields
General fields
| Field | Type | Unit | Description |
|---|---|---|---|
storage | integer | bytes | Total storage capacity of the bucket. |
object_count | integer | — | Total number of objects in the bucket. |
multi_part_upload_count | integer | — | Number of multipart upload tasks that have been initiated but not completed or aborted. |
live_channel_count | integer | — | Number of LiveChannels in the bucket. |
last_modified_time | integer | seconds (UNIX timestamp) | Point in time when the storage statistics were collected by this call. |
delete_marker_count | integer | — | Number of delete markers in the bucket. |
Per-storage-class fields
For Infrequent Access (IA), Archive Storage, Cold Archive, and Deep Cold Archive storage classes, OSS returns two separate capacity values: billable and actual. The billable capacity may differ from the actual capacity because OSS charges a minimum storage size for small objects in these classes.
| Field | Type | Unit | Description |
|---|---|---|---|
standard_storage | integer | bytes | Storage capacity of Standard objects. |
standard_object_count | integer | — | Number of Standard objects. |
infrequent_access_storage | integer | bytes | Billable storage capacity of Infrequent Access (IA) objects. |
infrequent_access_real_storage | integer | bytes | Actual storage capacity of IA objects. |
infrequent_access_object_count | integer | — | Number of IA objects. |
archive_storage | integer | bytes | Billable storage capacity of Archive Storage objects. |
archive_real_storage | integer | bytes | Actual storage capacity of Archive Storage objects. |
archive_object_count | integer | — | Number of Archive Storage objects. |
cold_archive_storage | integer | bytes | Billable storage capacity of Cold Archive objects. |
cold_archive_real_storage | integer | bytes | Actual storage capacity of Cold Archive objects. |
cold_archive_object_count | integer | — | Number of Cold Archive objects. |
deep_cold_archive_storage | integer | bytes | Billable storage capacity of Deep Cold Archive objects. |
deep_cold_archive_real_storage | integer | bytes | Actual storage capacity of Deep Cold Archive objects. |
deep_cold_archive_object_count | integer | — | Number of Deep Cold Archive objects. |