Lifecycle rules can apply to objects based on prefixes or object tags. You can also specify both a prefix and tags as conditions for a rule to take effect.
For a tag condition, both the key and value must match. If a rule includes both a prefix and multiple object tags, the rule applies only to objects that match the prefix and all specified tags.
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 OSS regions and endpoints.
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.
Add a tag-based matching rule to a lifecycle rule
The following code shows how to add a tag-based matching rule to a lifecycle rule.
# -*- coding: utf-8 -*-
import oss2
import datetime
from oss2.models import (LifecycleExpiration, LifecycleRule,
BucketLifecycle, AbortMultipartUpload,
TaggingRule, Tagging, StorageTransition)
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables. Before running the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the Endpoint for 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 information that corresponds to the Endpoint, such as cn-hangzhou. Note that this parameter is required for V4 signatures.
region = "cn-hangzhou"
# Replace examplebucket with the bucket name.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
# Configure a rule to expire objects 3 days after they are last modified.
# Set the rule name and the prefix for matching objects.
rule1 = LifecycleRule('rule1', 'tests/',
# Enable the expiration rule.
status=LifecycleRule.ENABLED,
# Set the expiration rule to expire objects 3 days after they are last modified.
expiration=LifecycleExpiration(days=3))
# Configure a rule to expire objects created before a specific date.
# Set the rule name and the prefix for matching objects.
rule2 = LifecycleRule('rule2', 'logging-',
# Disable the expiration rule.
status=LifecycleRule.DISABLED,
# Set the expiration rule to expire objects created before a specific date.
expiration=LifecycleExpiration(created_before_date=datetime.date(2018, 12, 12)))
# Configure a rule to expire parts 3 days after they are created.
rule3 = LifecycleRule('rule3', 'tests1/',
status=LifecycleRule.ENABLED,
abort_multipart_upload=AbortMultipartUpload(days=3))
# Configure a rule to expire parts created before a specific date.
rule4 = LifecycleRule('rule4', 'logging1-',
status=LifecycleRule.DISABLED,
abort_multipart_upload = AbortMultipartUpload(created_before_date=datetime.date(2018, 12, 12)))
# Set the tags for matching objects.
tagging_rule = TaggingRule()
tagging_rule.add('key1', 'value1')
tagging_rule.add('key2', 'value2')
tagging = Tagging(tagging_rule)
# Configure a transition rule to convert objects to Archive Storage 365 days after they are last modified.
# In rule5, tags are specified for matching objects. This rule applies only to objects that have both the key1=value1 and key2=value2 tags.
rule5 = LifecycleRule('rule5', 'logging2-',
status=LifecycleRule.ENABLED,
storage_transitions=[StorageTransition(days=365, storage_class=oss2.BUCKET_STORAGE_CLASS_ARCHIVE)],
tagging = tagging)
lifecycle = BucketLifecycle([rule1, rule2, rule3, rule4, rule5])
bucket.put_bucket_lifecycle(lifecycle)View tag information in a lifecycle rule
The following code shows how to view the tag information in a lifecycle rule.
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables. Before running the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the Endpoint for 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 information that corresponds to the Endpoint, such as cn-hangzhou. Note that this parameter is required for V4 signatures.
region = "cn-hangzhou"
# Replace examplebucket with the bucket name.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
# View the lifecycle rules.
lifecycle = bucket.get_bucket_lifecycle()
for rule in lifecycle.rules:
# View the rule for expiring parts.
if rule.abort_multipart_upload is not None:
print('id={0}, prefix={1}, tagging={2}, status={3}, days={4}, created_before_date={5}'
.format(rule.id, rule.prefix, rule.tagging, rule.status,
rule.abort_multipart_upload.days,
rule.abort_multipart_upload.created_before_date))
# View the rule for expiring objects.
if rule.expiration is not None:
print('id={0}, prefix={1}, tagging={2}, status={3}, days={4}, created_before_date={5}'
.format(rule.id, rule.prefix, rule.tagging, rule.status,
rule.expiration.days,
rule.expiration.created_before_date))
# View the transition rule.
if len(rule.storage_transitions) > 0:
storage_trans_info = ''
for storage_rule in rule.storage_transitions:
storage_trans_info += 'days={0}, created_before_date={1}, storage_class={2} **** '.format(
storage_rule.days, storage_rule.created_before_date, storage_rule.storage_class)
print('id={0}, prefix={1}, tagging={2}, status={3},, StorageTransition={4}'
.format(rule.id, rule.prefix, rule.tagging, rule.status, storage_trans_info))References
For the complete sample code for lifecycle rules, see the GitHub example.
For more information about the API operation to set lifecycle rules, see PutBucketLifecycle.
For more information about the API operation to view lifecycle rules, see GetBucketLifecycle.