全部產品
Search
文件中心

Object Storage Service:擷取儲存空間的儲存容量(Python SDK V2)

更新時間:Jul 31, 2025

本文介紹如何擷取指定儲存空間(Bucket)的儲存容量以及Bucket內不同儲存類型檔案(Object)的數量及其儲存容量。

注意事項

  • 本文範例程式碼以華東1(杭州)的地區IDcn-hangzhou為例,預設使用外網Endpoint,如果您希望通過與OSS同地區的其他阿里雲產品訪問OSS,請使用內網Endpoint。關於OSS支援的Region與Endpoint的對應關係,請參見OSS地區和訪問網域名稱

方法定義

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

請求參數列表

參數名

類型

說明

request

GetBucketStatRequest

佈建要求參數,具體請參見GetBucketStatRequest

傳回值列表

類型

說明

GetBucketStatResult

傳回值,具體請參見GetBucketStatResult

關於擷取儲存空間儲存容量的完整方法定義,請參見get_bucket_stat

範例程式碼

您可以使用以下代碼擷取儲存空間的儲存容量資訊。

import argparse
import alibabacloud_oss_v2 as oss

# 建立命令列參數解析器,描述此指令碼用於擷取指定Bucket的統計資訊。
parser = argparse.ArgumentParser(description="Get statistical information about a specified OSS bucket.")

# 添加命令列參數 --region,表示儲存空間所在的地區,必需參數
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)

# 添加命令列參數 --bucket,表示儲存空間的名稱,必需參數
parser.add_argument('--bucket', help='The name of the bucket to get statistics for.', required=True)

# 添加命令列參數 --endpoint,表示其他服務可用來訪問OSS的網域名稱,非必需參數
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS.')

def main():
    """
    主函數,用於解析命令列參數並擷取指定Bucket的統計資訊。
    """

    args = parser.parse_args()  # 解析命令列參數

    # 從環境變數中載入憑證資訊,用於身分識別驗證
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # 使用SDK的預設配置,並設定憑證提供者和地區資訊
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region

    # 如果提供了endpoint參數,則設定配置中的endpoint
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # 使用配置好的資訊建立OSS用戶端
    client = oss.Client(cfg)

    # 構造請求以擷取指定Bucket的統計資訊
    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()  # 指令碼入口,當檔案被直接運行時調用main函數

常用儲存容量資訊列表

參數

描述

Storage

儲存空間總儲存量,單位為位元組

ObjectCount

儲存空間中總的Object數量

MultipartUploadCount

儲存空間中已經初始化但還未完成或者還未中止的分區上傳數量

LiveChannelCount

儲存空間中Live Channel的數量

LastModifiedTime

此次調用擷取到的儲存資訊的時間點,格式為時間戳記,單位為秒

StandardStorage

標準儲存類型Object的儲存量,單位為位元組

StandardObjectCount

標準儲存類型Object數量

InfrequentAccessStorage

低頻儲存類型Object的計費儲存量,單位為位元組

InfrequentAccessRealStorage

低頻儲存類型Object的實際儲存量,單位為位元組

InfrequentAccessObjectCount

低頻儲存類型Object數量

ArchiveStorage

Archive Storage類型Object的計費儲存量,單位為位元組

ArchiveRealStorage

Archive Storage類型Object的實際儲存量,單位為位元組

ArchiveObjectCount

Archive Storage類型Object數量

ColdArchiveStorage

冷Archive Storage類型Object的計費儲存量,單位為位元組

ColdArchiveRealStorage

冷Archive Storage類型Object的實際儲存量,單位為位元組

ColdArchiveObjectCount

冷Archive Storage類型Object數量

相關文檔