All Products
Search
Document Center

Object Storage Service:Retention policies

Last Updated:Mar 17, 2025

You can configure a time-based retention policy for an Object Storage Service (OSS) bucket. The retention policy has a retention period that ranges from 1 day to 70 years. This topic describes how to create, query, lock, and cancel a retention policy.

Usage notes

  • The sample code in this topic uses the region ID cn-hangzhou of the China (Hangzhou) region. By default, a 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 OSS regions and endpoints, see Regions and endpoints.

Sample code

Create a retention policy

Important

A bucket cannot have versioning and retention policies configured at the same time. Therefore, before you create a retention policy for a bucket, make sure that versioning is not enabled for the bucket.

The following sample code provides an example on how to create a retention policy:

import argparse
import alibabacloud_oss_v2 as oss

# Create a command line parameter parser and describe the purpose of the script. The example describes how to use the Write Once Read Many (WORM) feature to create a retention policy for a bucket.
parser = argparse.ArgumentParser(description="initiate bucket worm sample")

# Specify the --region parameter, which specifies the region in which the bucket is located. This command line parameter is required.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Specify the --bucket parameter, which specifies the name of the bucket. This command line parameter is required.
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
# Specify the --endpoint parameter, which specifies the endpoint of the region in which the bucket is located. This command line parameter is optional.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
# Specify the --retention_period_in_days parameter, which specifies the number of days for which objects can be retained. This command line parameter is required.
parser.add_argument('--retention_period_in_days', help='The number of days for which objects can be retained.', required=True)

def main():
    # Parse the command line parameters to obtain the values specified by the user.
    args = parser.parse_args()

    # Load the authentication information required to access OSS from the environment variables.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Use the default configurations of the SDK to create a configuration object and specify the credential provider.
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region

    # If a custom endpoint is provided, modify the endpoint parameter in the configuration object.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Use the preceding configurations to initialize the OSSClient instance and allow the instance to interact with OSS.
    client = oss.Client(cfg)

    # Send a request to create a retention policy for the bucket.
    result = client.initiate_bucket_worm(oss.InitiateBucketWormRequest(
        bucket=args.bucket, # The name of the bucket.
        initiate_worm_configuration=oss.InitiateWormConfiguration(
            retention_period_in_days=int(args.retention_period_in_days), # The number of days for which objects can be retained. Set the value to an integer.
        ),
    ))

    # Display the HTTP status code of the operation, request ID, and retention policy ID to check the request status.
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          f' worm id: {result.worm_id}'
    )

# Call the main function to start the processing logic when the script is directly run.
if __name__ == "__main__":
    main() # Specify the entry points in the functions of the script. The control program flow starts here.

Cancel an unlocked retention policy

The following sample code provides an example on how to cancel an unlocked retention policy:

import argparse
import alibabacloud_oss_v2 as oss

# Create a command line parameter parser and describe the purpose of the script. The example describes how to use the WORM feature to cancel an unlocked retention policy of a bucket.
parser = argparse.ArgumentParser(description="abort bucket worm sample")

# Specify the --region parameter, which specifies the region in which the bucket is located. This command line parameter is required.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Specify the --bucket parameter, which specifies the name of the bucket. This command line parameter is required.
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
# Specify the --endpoint parameter, which specifies the endpoint of the region in which the bucket is located. This command line parameter is optional.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')

def main():
    # Parse the command line parameters to obtain the values specified by the user.
    args = parser.parse_args()

    # Load the authentication information required to access OSS from the environment variables.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Use the default configurations of the SDK to create a configuration object and specify the credential provider.
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region

    # If a custom endpoint is provided, modify the endpoint parameter in the configuration object.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Use the preceding configurations to initialize the OSSClient instance and allow the instance to interact with OSS.
    client = oss.Client(cfg)

    # Send a request to cancel an unlocked retention policy of the bucket.
    result = client.abort_bucket_worm(oss.AbortBucketWormRequest(
        bucket=args.bucket, # The name of the bucket.
    ))

    # Display the HTTP status code of the operation result and request ID to check the request status.
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id}'
    )

# Call the main function to start the processing logic when the script is directly run.
if __name__ == "__main__":
    main() # Specify the entry points in functions of the script. The control program flow starts here.

Lock a retention policy

The following sample code provides an example on how to lock a retention policy:

import argparse
import alibabacloud_oss_v2 as oss

# Create a command line parameter parser and describe the purpose of the script. The example describes how to use the WORM feature to lock a retention policy of a bucket.
parser = argparse.ArgumentParser(description="complete bucket worm sample")

# Specify the --region parameter, which specifies the region in which the bucket is located. This command line parameter is required.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Specify the --bucket parameter, which specifies the name of the bucket. This command line parameter is required.
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
# Specify the --endpoint parameter, which specifies the endpoint of the region in which the bucket is located. This command line parameter is optional.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
# Specify the --worm_id parameter, which specifies the ID of the retention policy that you want to lock. This command line parameter is required.
parser.add_argument('--worm_id', help='The ID of the retention policy.', required=True)

def main():
    # Parse the command line parameters to obtain the values specified by the user.
    args = parser.parse_args()

    # Load the authentication information required to access OSS from the environment variables.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Use the default configurations of the SDK to create a configuration object and specify the credential provider.
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region

    # If a custom endpoint is provided, modify the endpoint parameter in the configuration object.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Use the preceding configurations to initialize the OSSClient instance and allow the instance to interact with OSS.
    client = oss.Client(cfg)

    # Send a request to lock a retention policy of the bucket.
    result = client.complete_bucket_worm(oss.CompleteBucketWormRequest(
        bucket=args.bucket, # The name of the bucket.
        worm_id=args.worm_id,  # The ID of the retention policy that you want to lock.
    ))

    # Display the HTTP status code of the operation result and request ID to check the request status.
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id}'
    )

# Call the main function to start the processing logic when the script is directly run.
if __name__ == "__main__":
    main() # Specify the entry points in functions of the script. The control program flow starts here.

Query a retention policy

The following sample code provides an example on how to query a retention policy:

import argparse
import alibabacloud_oss_v2 as oss

# Create a command line parameter parser and describe the purpose of the script. The example describes how to use the WORM feature to query a retention policy of a bucket.
parser = argparse.ArgumentParser(description="get bucket worm sample")

# Specify the --region parameter, which specifies the region in which the bucket is located. This command line parameter is required.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Specify the --bucket parameter, which specifies the name of the bucket. This command line parameter is required.
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
# Specify the --endpoint parameter, which specifies the endpoint of the region in which the bucket is located. This command line parameter is optional.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')

def main():
    # Parse the command line parameters to obtain the values specified by the user.
    args = parser.parse_args()

    # Load the authentication information required to access OSS from the environment variables.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Use the default configurations of the SDK to create a configuration object and specify the credential provider.
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region

    # If a custom endpoint is provided, modify the endpoint parameter in the configuration object.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Use the preceding configurations to initialize the OSSClient instance and allow the instance to interact with OSS.
    client = oss.Client(cfg)

    # Send a request to query a retention policy of the bucket.
    result = client.get_bucket_worm(oss.GetBucketWormRequest(
        bucket=args.bucket, # The name of the bucket.
    ))

    # Display the HTTP status code of the operation result, request ID, and the configurations of the retention policy.
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          f' worm id: {result.worm_configuration.worm_id},' # The ID of the retention policy.
          f' state: {result.worm_configuration.state},' # The state of the WORM feature.
          f' retention period in days: {result.worm_configuration.retention_period_in_days},' # The number of days for which the objects can be retained.
          f' creation date: {result.worm_configuration.creation_date},' # The date on which the retention policy is created.
          f' expiration date: {result.worm_configuration.expiration_date},' # The date on which the retention policy expires.
    )

# Call the main function to start the processing logic when the script is directly run.
if __name__ == "__main__":
    main() # Specify the entry points in functions of the script. The control program flow starts here.

Extend the retention period of a retention policy

The following sample code provides an example on how to extend the retention period of a retention policy:

import argparse
import alibabacloud_oss_v2 as oss

# Create a command line parameter parser and describe the purpose of the script. The example describes how to use the WORM feature to extend the retention period of a retention policy.
parser = argparse.ArgumentParser(description="extend bucket worm sample")

# Specify the --region parameter, which specifies the region in which the bucket is located. This command line parameter is required.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Specify the --bucket parameter, which specifies the name of the bucket. This command line parameter is required.
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
# Specify the --endpoint parameter, which specifies the endpoint of the region in which the bucket is located. This command line parameter is optional.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
# Specify the --worm_id parameter, which specifies the ID of the retention policy whose retention period you want to extend. This command line parameter is required.
parser.add_argument('--worm_id', help='The ID of the retention policy.', required=True)
# Specify the --retention_period_in_days parameter, which specifies the number of days for which the objects in the bucket can be retained. This command line parameter is required.
parser.add_argument('--retention_period_in_days', help='The number of days for which objects can be retained.', required=True)

def main():
    # Parse the command line parameters to obtain the values specified by the user.
    args = parser.parse_args()

    # Load the authentication information required to access OSS from the environment variables.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Use the default configurations of the SDK to create a configuration object and specify the credential provider.
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region

    # If a custom endpoint is provided, modify the endpoint parameter in the configuration object.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Use the preceding configurations to initialize the OSSClient instance and allow the instance to interact with OSS.
    client = oss.Client(cfg)

    # Send a request to extend the retention policy of the bucket.
    result = client.extend_bucket_worm(oss.ExtendBucketWormRequest(
        bucket=args.bucket, # The name of the bucket.
        worm_id=args.worm_id,  # The ID of the retention policy.
        extend_worm_configuration=oss.ExtendWormConfiguration(
            retention_period_in_days=int(args.retention_period_in_days), # The number of days for which objects can be retained. Set the value to an integer.
        ),
    ))

    # Display the HTTP status code of the operation result and request ID to check the request status.
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id}'
    )

# Call the main function to start the processing logic when the script is directly run.
if __name__ == "__main__":
    main() # Specify the entry points in functions of the script. The control program flow starts here.

References

  • For more information about the common errors in retention policies, see 27-WORM.