全部產品
Search
文件中心

Object Storage Service:合規保留原則(Python SDK V2)

更新時間:Aug 01, 2025

Object Storage Service允許針對儲存空間(Bucket)設定基於時間的合規保留原則,保護周期為1天到70年。本文介紹如何建立、擷取、鎖定合規保留原則等。

注意事項

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

範例程式碼

建立合規保留原則

重要

同一Bucket中,版本控制與合規保留原則無法同時配置。因此,當您需要為Bucket建立合規保留原則前,請確保此Bucket未開啟版本控制。

您可以使用以下代碼建立合規保留原則:

import argparse
import alibabacloud_oss_v2 as oss

# 建立命令列參數解析器,並描述指令碼用途:樣本展示如何初始化OSS儲存空間的WORM配置
parser = argparse.ArgumentParser(description="initiate bucket worm sample")

# 添加命令列參數 --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.', required=True)
# 添加命令列參數 --endpoint,表示其他服務可用來訪問OSS的網域名稱,非必需參數
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
# 添加命令列參數 --retention_period_in_days,表示對象保留天數,必需參數
parser.add_argument('--retention_period_in_days', help='The number of days for which objects can be retained.', required=True)

def main():
    # 解析命令列提供的參數,擷取使用者輸入的值
    args = parser.parse_args()

    # 從環境變數中載入訪問OSS所需的認證資訊,用於身分識別驗證
    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用戶端,準備與OSS互動
    client = oss.Client(cfg)

    # 發送請求以初始化指定儲存空間的WORM配置
    result = client.initiate_bucket_worm(oss.InitiateBucketWormRequest(
        bucket=args.bucket,  # 儲存空間名
        initiate_worm_configuration=oss.InitiateWormConfiguration(
            retention_period_in_days=int(args.retention_period_in_days),  # 對象保留天數,轉換為整數
        ),
    ))

    # 列印操作結果的狀態代碼、請求ID和WORM ID,以便確認請求狀態
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          f' worm id: {result.worm_id}'
    )

# 當此指令碼被直接執行時,調用main函數開始處理邏輯
if __name__ == "__main__":
    main()  # 指令碼進入點,控製程序流程從這裡開始

取消未鎖定的合規保留原則

您可以使用以下代碼取消未鎖定的合規保留原則:

import argparse
import alibabacloud_oss_v2 as oss

# 建立命令列參數解析器,並描述指令碼用途:樣本展示如何中止OSS儲存空間的WORM配置
parser = argparse.ArgumentParser(description="abort bucket worm sample")

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

def main():
    # 解析命令列提供的參數,擷取使用者輸入的值
    args = parser.parse_args()

    # 從環境變數中載入訪問OSS所需的認證資訊,用於身分識別驗證
    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用戶端,準備與OSS互動
    client = oss.Client(cfg)

    # 發送請求以中止指定儲存空間的WORM配置
    result = client.abort_bucket_worm(oss.AbortBucketWormRequest(
        bucket=args.bucket,  # 儲存空間名
    ))

    # 列印操作結果的狀態代碼和請求ID,以便確認請求狀態
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id}'
    )

# 當此指令碼被直接執行時,調用main函數開始處理邏輯
if __name__ == "__main__":
    main()  # 指令碼進入點,控製程序流程從這裡開始

鎖定合規保留原則

您可以使用以下代碼鎖定合規保留原則:

import argparse
import alibabacloud_oss_v2 as oss

# 建立命令列參數解析器,並描述指令碼用途:樣本展示如何完成OSS儲存空間的WORM配置
parser = argparse.ArgumentParser(description="complete bucket worm sample")

# 添加命令列參數 --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.', required=True)
# 添加命令列參數 --endpoint,表示其他服務可用來訪問OSS的網域名稱,非必需參數
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
# 添加命令列參數 --worm_id,表示要完成的WORM策略ID,必需參數
parser.add_argument('--worm_id', help='The ID of the retention policy.', required=True)

def main():
    # 解析命令列提供的參數,擷取使用者輸入的值
    args = parser.parse_args()

    # 從環境變數中載入訪問OSS所需的認證資訊,用於身分識別驗證
    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用戶端,準備與OSS互動
    client = oss.Client(cfg)

    # 發送請求以完成指定儲存空間的WORM配置
    result = client.complete_bucket_worm(oss.CompleteBucketWormRequest(
        bucket=args.bucket,  # 儲存空間名
        worm_id=args.worm_id,  # WORM策略ID
    ))

    # 列印操作結果的狀態代碼和請求ID,以便確認請求狀態
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id}'
    )

# 當此指令碼被直接執行時,調用main函數開始處理邏輯
if __name__ == "__main__":
    main()  # 指令碼進入點,控製程序流程從這裡開始

擷取合規保留原則

您可以使用以下代碼擷取合規保留原則配置資訊:

import argparse
import alibabacloud_oss_v2 as oss

# 建立命令列參數解析器,並描述指令碼用途:樣本展示如何擷取OSS儲存空間的WORM配置資訊
parser = argparse.ArgumentParser(description="get bucket worm sample")

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

def main():
    # 解析命令列提供的參數,擷取使用者輸入的值
    args = parser.parse_args()

    # 從環境變數中載入訪問OSS所需的認證資訊,用於身分識別驗證
    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用戶端,準備與OSS互動
    client = oss.Client(cfg)

    # 發送請求以擷取指定儲存空間的WORM配置資訊
    result = client.get_bucket_worm(oss.GetBucketWormRequest(
        bucket=args.bucket,  # 儲存空間名
    ))

    # 列印操作結果的狀態代碼、請求ID以及WORM配置詳情
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          f' worm id: {result.worm_configuration.worm_id},'  # WORM策略ID
          f' state: {result.worm_configuration.state},'  # 當前WORM狀態
          f' retention period in days: {result.worm_configuration.retention_period_in_days},'  # 對象保留天數
          f' creation date: {result.worm_configuration.creation_date},'  # 建立日期
          f' expiration date: {result.worm_configuration.expiration_date},'  # 到期日期
    )

# 當此指令碼被直接執行時,調用main函數開始處理邏輯
if __name__ == "__main__":
    main()  # 指令碼進入點,控製程序流程從這裡開始

延長Object的保護天數

以下代碼用於延長鎖定的合規保留原則中Object的保護天數:

import argparse
import alibabacloud_oss_v2 as oss

# 建立命令列參數解析器,並描述指令碼用途:樣本展示如何延長OSS儲存空間的WORM配置的保留期限
parser = argparse.ArgumentParser(description="extend bucket worm sample")

# 添加命令列參數 --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.', required=True)
# 添加命令列參數 --endpoint,表示其他服務可用來訪問OSS的網域名稱,非必需參數
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
# 添加命令列參數 --worm_id,表示要延長的WORM策略ID,必需參數
parser.add_argument('--worm_id', help='The ID of the retention policy.', required=True)
# 添加命令列參數 --retention_period_in_days,表示新的對象保留天數,必需參數
parser.add_argument('--retention_period_in_days', help='The number of days for which objects can be retained.', required=True)

def main():
    # 解析命令列提供的參數,擷取使用者輸入的值
    args = parser.parse_args()

    # 從環境變數中載入訪問OSS所需的認證資訊,用於身分識別驗證
    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用戶端,準備與OSS互動
    client = oss.Client(cfg)

    # 發送請求以延長指定儲存空間的WORM配置的保留期限
    result = client.extend_bucket_worm(oss.ExtendBucketWormRequest(
        bucket=args.bucket,  # 儲存空間名
        worm_id=args.worm_id,  # WORM策略ID
        extend_worm_configuration=oss.ExtendWormConfiguration(
            retention_period_in_days=int(args.retention_period_in_days),  # 新的對象保留天數,轉換為整數
        ),
    ))

    # 列印操作結果的狀態代碼和請求ID,以便確認請求狀態
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id}'
    )

# 當此指令碼被直接執行時,調用main函數開始處理邏輯
if __name__ == "__main__":
    main()  # 指令碼進入點,控製程序流程從這裡開始

相關文檔

  • 關於合規保留原則的常見報錯排查,請查閱27-WORM