All Products
Search
Document Center

Object Storage Service:Query the storage capacity of a bucket (Python SDK V1)

Last Updated:Aug 08, 2025

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 and endpoints.

  • In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.

  • 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 Initialization.

  • To query the storage usage of a bucket, you must have the oss:GetBucketStat permission. For more information, see Attach a custom policy to a RAM user.

Sample 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.

Important

Only Python SDK 2.16.0 and later support all the properties returned in the following sample code.

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# 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.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"

# Specify the region where the bucket is located, such as cn-hangzhou. Note: This parameter is required for V4 signatures.
region = "cn-hangzhou"

# Set the bucket name to yourBucketName.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

# Query the statistics information of the bucket.
result = bucket.get_bucket_stat()
# Query the total storage capacity of the bucket in bytes.
print(result.storage_size_in_bytes)
# Query the total number of objects in the bucket.
print(result.object_count)
# Query the number of multipart upload tasks that are initiated but not completed or aborted.
print(result.multi_part_upload_count)
# Query the number of live channels in the bucket.
print(result.live_channel_count)
# The time when the storage information was obtained. The value is a UNIX timestamp. Unit: seconds.
print(result.last_modified_time)
# Query the storage capacity of Standard objects in bytes.
print(result.standard_storage)
# Query the number of Standard objects.
print(result.standard_object_count)
# Query the billable storage capacity of Infrequent Access (IA) objects in bytes.
print(result.infrequent_access_storage)
# Query the actual storage capacity of IA objects in bytes.
print(result.infrequent_access_real_storage)
# Query the number of IA objects.
print(result.infrequent_access_object_count)
# Query the billable storage capacity of Archive objects in bytes.
print(result.archive_storage)
# Query the actual storage capacity of Archive objects in bytes.
print(result.archive_real_storage)
# Query the number of Archive objects.
print(result.archive_object_count)
# Query the billable storage capacity of Cold Archive objects in bytes.
print(result.cold_archive_storage)
# Query the actual storage capacity of Cold Archive objects in bytes.
print(result.cold_archive_real_storage)
# Query the number of Cold Archive objects.
print(result.cold_archive_object_count)

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.