All Products
Search
Document Center

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

Last Updated:Oct 21, 2025

This topic describes how to query the storage capacity of a specified bucket, as well as the number and storage capacity of objects for different storage classes in the bucket.

Usage notes

  • The sample code in this topic uses the region ID cn-hangzhou of the China (Hangzhou) region. By default, the public endpoint is used to access resources in a bucket. If you want to access resources in the bucket by using other Alibaba Cloud services in the same region in which the bucket is located, use an internal endpoint. For more information about the regions and endpoints supported by OSS, see Regions and endpoints.

Method definition

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

Request parameters

Parameter

Type

Description

request

GetBucketStatRequest

The request parameters. For more information, see GetBucketStatRequest

Response parameters

Type

Description

GetBucketStatResult

The response parameters. For more information, see GetBucketStatResult

For the complete method definition, see get_bucket_stat.

Sample code

You can use the following code to query the storage capacity of a bucket.

import argparse
import alibabacloud_oss_v2 as oss

# Create a command-line argument parser to describe that this script is used to obtain statistical information about a specified bucket.
parser = argparse.ArgumentParser(description="Get statistical information about a specified OSS bucket.")

# Add the --region command-line argument, which specifies the region where the bucket is located. This argument is required.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)

# Add the --bucket command-line argument, which specifies the name of the bucket for which to obtain statistics. This argument is required.
parser.add_argument('--bucket', help='The name of the bucket to get statistics for.', required=True)

# Add the --endpoint command-line argument, which specifies the domain name that other services can use to access OSS. This argument is optional.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS.')

def main():
    """
    The main function, which is used to parse command-line arguments and obtain statistical information about a specified bucket.
    """

    args = parser.parse_args()  # Parse command-line arguments.

    # Load credentials from environment variables for identity verification.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Use the default configurations of the SDK and set the credential provider and region information.
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region

    # If the endpoint parameter is provided, set the endpoint in the configuration.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Create an OSS client using the configured information.
    client = oss.Client(cfg)

    # Construct a request to obtain statistical information about the specified bucket.
    request = oss.GetBucketStatRequest(bucket=args.bucket)

    # Send the request and obtain the response.
    result = client.get_bucket_stat(request)

    # Print the statistical information in the response.
    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()  # The script entry point. The main function is called when the file is run directly.

Common storage capacity information

Parameter

Description

Storage

The total storage capacity of the bucket. Unit: bytes.

ObjectCount

The total number of objects in the bucket.

MultipartUploadCount

The number of multipart upload tasks that have been initiated but not completed or aborted in the bucket.

LiveChannelCount

The number of LiveChannels in the bucket.

LastModifiedTime

The point in time when the storage information was obtained by this call. The value is a UNIX timestamp. Unit: seconds.

StandardStorage

The storage capacity of Standard objects. Unit: bytes.

StandardObjectCount

The number of Standard objects.

InfrequentAccessStorage

The billable storage capacity of Infrequent Access (IA) objects. Unit: bytes.

InfrequentAccessRealStorage

The actual storage capacity of IA objects. Unit: bytes.

InfrequentAccessObjectCount

The number of IA objects.

ArchiveStorage

The billable storage capacity of Archive Storage objects. Unit: bytes.

ArchiveRealStorage

The actual storage capacity of Archive Storage objects. Unit: bytes.

ArchiveObjectCount

The number of Archive Storage objects.

ColdArchiveStorage

The billable storage capacity of Cold Archive objects. Unit: bytes.

ColdArchiveRealStorage

The actual storage capacity of Cold Archive objects. Unit: bytes.

ColdArchiveObjectCount

The number of Cold Archive objects.

References