Use the OSS Python SDK v2 to manage write-once-read-many (WORM) compliance retention policies on a bucket. A retention policy enforces immutability for objects during the retention period, which can range from 1 day to 70 years.
This topic covers five operations: create, query, lock, extend, and cancel a retention policy.
Usage notes
All examples use the cn-hangzhou region. By default, the client connects using a public endpoint. To access OSS from another Alibaba Cloud service in the same region, set cfg.endpoint to the internal endpoint. For a full list of endpoints, see Regions and endpoints.
Prerequisites
Before you begin, make sure you have:
An OSS bucket
Python SDK v2 (
alibabacloud_oss_v2) installedAccess credentials configured as environment variables
Query a retention policy
Call get_bucket_worm() to retrieve the current configuration and status of the retention policy.
import argparse
import alibabacloud_oss_v2 as oss
parser = argparse.ArgumentParser(description="Query the WORM retention policy of a bucket")
parser.add_argument('--region', required=True, help='Region ID, e.g. cn-hangzhou')
parser.add_argument('--bucket', required=True, help='Bucket name')
parser.add_argument('--endpoint', help='Custom endpoint (optional)')
def main():
args = parser.parse_args()
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = args.region
if args.endpoint is not None:
cfg.endpoint = args.endpoint
client = oss.Client(cfg)
result = client.get_bucket_worm(oss.GetBucketWormRequest(
bucket=args.bucket,
))
print(f'status code: {result.status_code},'
f' request id: {result.request_id},'
f' worm id: {result.worm_configuration.worm_id},'
f' state: {result.worm_configuration.state},'
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},'
)
if __name__ == "__main__":
main()The worm_configuration object returns the following fields:
| Field | Description |
|---|---|
worm_id | Unique ID of the retention policy |
state | Current state of the WORM feature |
retention_period_in_days | Retention period in days |
creation_date | Date the policy was created |
expiration_date | Date the policy expires |
Extend the retention period
Call extend_bucket_worm() to increase the retention period of a retention policy.
import argparse
import alibabacloud_oss_v2 as oss
parser = argparse.ArgumentParser(description="Extend the retention period of a locked WORM policy")
parser.add_argument('--region', required=True, help='Region ID, e.g. cn-hangzhou')
parser.add_argument('--bucket', required=True, help='Bucket name')
parser.add_argument('--endpoint', help='Custom endpoint (optional)')
parser.add_argument('--worm_id', required=True, help='Worm ID of the locked policy')
parser.add_argument('--retention_period_in_days', required=True, help='New retention period in days')
def main():
args = parser.parse_args()
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = args.region
if args.endpoint is not None:
cfg.endpoint = args.endpoint
client = oss.Client(cfg)
result = client.extend_bucket_worm(oss.ExtendBucketWormRequest(
bucket=args.bucket,
worm_id=args.worm_id,
extend_worm_configuration=oss.ExtendWormConfiguration(
retention_period_in_days=int(args.retention_period_in_days),
),
))
print(f'status code: {result.status_code},'
f' request id: {result.request_id}'
)
if __name__ == "__main__":
main()What's next
For common errors related to retention policies, see 27-WORM.