Configure lifecycle rules
The following code provides examples of two lifecycle rules. One lifecycle rule is configured based on the last modified time of data. The other lifecycle rule is configured based on the last access time of data. After you configure lifecycle rules, you can modify the lifecycle rules based on your business requirements. For information about how to modify existing lifecycle rules, see Lifecycle rules based on the last modified time.
Configure a lifecycle rule based on the last modified time to change the storage classes of objects or delete objects
The following code provides an example on how to configure a lifecycle rule based on the last modified time to change the storage classes of objects or delete objects in the examplebucket bucket:
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
import datetime
from oss2.models import (LifecycleExpiration, LifecycleRule,
BucketLifecycle,AbortMultipartUpload,
TaggingRule, Tagging, StorageTransition,
NoncurrentVersionStorageTransition,
NoncurrentVersionExpiration)
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
region = "cn-hangzhou"
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
rule1 = LifecycleRule('rule1', 'tests/',
status=LifecycleRule.ENABLED,
expiration=LifecycleExpiration(days=3))
rule2 = LifecycleRule('rule2', 'tests2/',
status=LifecycleRule.ENABLED,
expiration = LifecycleExpiration(created_before_date=datetime.date(2023, 12, 12)))
rule3 = LifecycleRule('rule3', 'tests3/',
status=LifecycleRule.ENABLED,
abort_multipart_upload=AbortMultipartUpload(days=3))
rule4 = LifecycleRule('rule4', 'tests4/',
status=LifecycleRule.ENABLED,
abort_multipart_upload = AbortMultipartUpload(created_before_date=datetime.date(2022, 12, 12)))
rule5 = LifecycleRule('rule5', 'tests5/',
status=LifecycleRule.ENABLED,
storage_transitions=[StorageTransition(days=20,storage_class=oss2.BUCKET_STORAGE_CLASS_IA),
StorageTransition(days=30,storage_class=oss2.BUCKET_STORAGE_CLASS_ARCHIVE)])
tagging_rule = TaggingRule()
tagging_rule.add('key1', 'value1')
tagging_rule.add('key2', 'value2')
tagging = Tagging(tagging_rule)
rule6 = LifecycleRule('rule6', 'tests6/',
status=LifecycleRule.ENABLED,
storage_transitions=[StorageTransition(created_before_date=datetime.date(2022, 12, 12),storage_class=oss2.BUCKET_STORAGE_CLASS_IA)],
tagging = tagging)
rule7 = LifecycleRule('rule7', 'tests7/',
status=LifecycleRule.ENABLED,
storage_transitions=[StorageTransition(days=365, storage_class=oss2.BUCKET_STORAGE_CLASS_ARCHIVE)],
expiration=LifecycleExpiration(expired_detete_marker=True),
noncurrent_version_sotrage_transitions =
[NoncurrentVersionStorageTransition(12, oss2.BUCKET_STORAGE_CLASS_IA),
NoncurrentVersionStorageTransition(20, oss2.BUCKET_STORAGE_CLASS_ARCHIVE)],
noncurrent_version_expiration = NoncurrentVersionExpiration(30))
lifecycle = BucketLifecycle([rule1, rule2, rule3, rule4, rule5, rule6, rule7])
bucket.put_bucket_lifecycle(lifecycle)
Configure a lifecycle rule based on the last modified time to change the storage classes of objects, excluding the objects whose names contain specific prefixes or that have specific tags
The following sample code provides an example on how to configure a lifecycle rule based on the last modified time. The lifecycle rule changes the storage classes of objects that meet the following conditions in the examplebucket bucket to IA 30 days after the objects are last modified: The names of the objects do not contain the log prefix and the objects do not have the tag whose key is tag1 and whose value is value1. The conditions are specified in the Not element of the filter node.
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
from oss2.models import LifecycleRule, BucketLifecycle, StorageTransition, LifecycleFilter, FilterNot, FilterNotTag
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
region = "cn-hangzhou"
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
not_prefix = 'logs/not-prefix'
key = 'key1'
value = 'value1'
not_tag = FilterNotTag(key, value)
filter_not = FilterNot(not_prefix, not_tag)
filter = LifecycleFilter([filter_not])
rule1 = LifecycleRule('mtime transition1', 'logs',
status=LifecycleRule.ENABLED,
storage_transitions=[StorageTransition(days=30, storage_class=oss2.BUCKET_STORAGE_CLASS_IA)],
filter=filter)
lifecycle = BucketLifecycle([rule1])
result = bucket.put_bucket_lifecycle(lifecycle)
print('The lifecycle configuration is successful. ' + str(result.status))
Configure a lifecycle rule based on the last modified time to change the storage classes of objects
The following code provides an example on how to configure a lifecycle rule based on the last access time to change the storage classes of objects in the examplebucket bucket:
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
from oss2.models import LifecycleRule, BucketLifecycle, StorageTransition, NoncurrentVersionStorageTransition
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
region = "cn-hangzhou"
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
rule1 = LifecycleRule('rule1', 'logs', status=LifecycleRule.ENABLED)
rule1.storage_transitions = [StorageTransition(days=30, storage_class=oss2.BUCKET_STORAGE_CLASS_IA, is_access_time=True, return_to_std_when_visit=False, allow_small_file=True)]
rule2 = LifecycleRule('rule2', 'dir', status=LifecycleRule.ENABLED)
rule2.noncurrent_version_sotrage_transitions = [NoncurrentVersionStorageTransition(10, oss2.BUCKET_STORAGE_CLASS_IA, is_access_time=True, return_to_std_when_visit=True, allow_small_file=False)]
lifecycle = BucketLifecycle([rule1, rule2])
result = bucket.put_bucket_lifecycle(lifecycle)
print('The lifecycle configuration is successful. ' + str(result.status))
Query lifecycle rules
The following code provides an example on how to query the lifecycle rules configured for the bucket named examplebucket:
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
region = "cn-hangzhou"
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
lifecycle = bucket.get_bucket_lifecycle()
for rule in lifecycle.rules:
print('==========')
print('id:', rule.id)
print('prefix:', rule.prefix)
print('status', rule.status)
if rule.tagging is not None:
print('tagging:', rule.tagging)
if rule.abort_multipart_upload is not None:
if rule.abort_multipart_upload.days is not None:
print('abort_multipart_upload days:', rule.abort_multipart_upload.days)
else:
print('abort_multipart_upload created_before_date:', rule.abort_multipart_upload.created_before_date)
if rule.expiration is not None:
if rule.expiration.days is not None:
print('expiration days:', rule.expiration.days)
elif rule.expiration.expired_detete_marker is not None:
print('expiration expired_detete_marker:', rule.expiration.expired_detete_marker)
elif rule.expiration.created_before_date is not None:
print('expiration created_before_date:', rule.expiration.created_before_date)
if len(rule.storage_transitions) > 0:
storage_info = ''
for storage_rule in rule.storage_transitions:
if storage_rule.days is not None:
storage_info += 'days={0}, storage_class={1} *** '.format(
storage_rule.days, storage_rule.storage_class)
else:
storage_info += 'created_before_date={0}, storage_class={1} *** '.format(
storage_rule.created_before_date, storage_rule.storage_class)
storage_info += 'is_access_time={0}, return_to_std_when_visit={1} ***'.format(
storage_rule.is_access_time, storage_rule.return_to_std_when_visit)
print('storage_transitions:', storage_info)
if len(rule.noncurrent_version_sotrage_transitions) > 0:
noncurrent_storage_info = ''
for storage_rule in rule.noncurrent_version_sotrage_transitions:
noncurrent_storage_info += 'days={0}, storage_class={1} *** '.format(
storage_rule.noncurrent_days, storage_rule.storage_class)
noncurrent_storage_info += 'is_access_time={0}, return_to_std_when_visit={1} ***'.format(
storage_rule.is_access_time, storage_rule.return_to_std_when_visit)
print('noncurrent_version_sotrage_transitions:', noncurrent_storage_info)
if rule.noncurrent_version_expiration is not None:
print('noncurrent_version_expiration days:', rule.noncurrent_version_expiration.noncurrent_days)