All Products
Search
Document Center

Object Storage Service:Query the storage capacity of a bucket using OSS SDK for Python 2.0

Last Updated:Mar 20, 2026

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_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables set with valid credentials

  • OSS 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 endpoint parameter to the internal endpoint. For supported regions and endpoints, see Regions and endpoints.

Method definition

get_bucket_stat(request: GetBucketStatRequest, **kwargs) → GetBucketStatResult

Request parameters

ParameterTypeDescription
requestGetBucketStatRequestThe request parameters. See GetBucketStatRequest.

Response type

TypeDescription
GetBucketStatResultThe 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

FieldTypeUnitDescription
storageintegerbytesTotal storage capacity of the bucket.
object_countintegerTotal number of objects in the bucket.
multi_part_upload_countintegerNumber of multipart upload tasks that have been initiated but not completed or aborted.
live_channel_countintegerNumber of LiveChannels in the bucket.
last_modified_timeintegerseconds (UNIX timestamp)Point in time when the storage statistics were collected by this call.
delete_marker_countintegerNumber 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.

FieldTypeUnitDescription
standard_storageintegerbytesStorage capacity of Standard objects.
standard_object_countintegerNumber of Standard objects.
infrequent_access_storageintegerbytesBillable storage capacity of Infrequent Access (IA) objects.
infrequent_access_real_storageintegerbytesActual storage capacity of IA objects.
infrequent_access_object_countintegerNumber of IA objects.
archive_storageintegerbytesBillable storage capacity of Archive Storage objects.
archive_real_storageintegerbytesActual storage capacity of Archive Storage objects.
archive_object_countintegerNumber of Archive Storage objects.
cold_archive_storageintegerbytesBillable storage capacity of Cold Archive objects.
cold_archive_real_storageintegerbytesActual storage capacity of Cold Archive objects.
cold_archive_object_countintegerNumber of Cold Archive objects.
deep_cold_archive_storageintegerbytesBillable storage capacity of Deep Cold Archive objects.
deep_cold_archive_real_storageintegerbytesActual storage capacity of Deep Cold Archive objects.
deep_cold_archive_object_countintegerNumber of Deep Cold Archive objects.