All Products
Search
Document Center

Object Storage Service:Use object tagging for lifecycle management

Last Updated:Nov 01, 2024

You can configure lifecycle rules for objects with specified prefixes or tags. You can also specify a combination of prefixes and tags as conditions of lifecycle rules.

Note

If you configure tag conditions, the rule applies only to objects that meet the tag key and value conditions. If a prefix and multiple object tags are configured in a rule, the rule applies only to objects that match the prefix and object tag conditions.

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 Regions, endpoints and open ports.

  • 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.

Specify tags in a lifecycle rule

The following code provides an example on how to specify tags in 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 you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)

# Specify that objects expire three days after they are last modified. 
# Specify the name of the expiration rule and the prefix to match the objects. 
rule1 = LifecycleRule('rule1', 'tests/',
                      # Enable the expiration rule. 
                      status=LifecycleRule.ENABLED,
                      # Specify that objects expire three days after they are last modified. 
                      expiration=LifecycleExpiration(days=3))

# Specify that the objects last modified before the specified date expire. 
# Specify the name of the expiration rule and the prefix to match the objects. 
rule2 = LifecycleRule('rule2', 'logging-',
                      # Disable the expiration rule. 
                      status=LifecycleRule.DISABLED,
                      # Specify that the objects last modified before the specified date expire. 
                      expiration=LifecycleExpiration(created_before_date=datetime.date(2018, 12, 12)))

# Specify that parts expire three days after they are last modified. 
rule3 = LifecycleRule('rule3', 'tests1/',
                      status=LifecycleRule.ENABLED,
                      abort_multipart_upload=AbortMultipartUpload(days=3))

# Specify that the parts last modified before the specified date expire. 
rule4 = LifecycleRule('rule4', 'logging1-',
                      status=LifecycleRule.DISABLED,
                      abort_multipart_upload = AbortMultipartUpload(created_before_date=datetime.date(2018, 12, 12)))

# Configure tags to match objects. 
tagging_rule = TaggingRule()
tagging_rule.add('key1', 'value1')
tagging_rule.add('key2', 'value2')
tagging = Tagging(tagging_rule)

# Configure the rule to convert the storage class of an object. Specify that the storage class of an object is converted to Archive 365 days after the object is last modified.  
# Tags that match objects are specified in rule5. The rule applies only to objects that match tag conditions of key1=value1 and key2=value2. 
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)

Query tags configured in a lifecycle rule

The following code provides an example on how to query tags configured in a lifecycle rule:

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)

# Query the lifecycle rules. 
lifecycle = bucket.get_bucket_lifecycle()

for rule in lifecycle.rules:
    # Query the part expiration rules. 
    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))

    # Query the object expiration rules. 
    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))
    # Query the rules that convert the storage class of an object. 
    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 that is used to manage lifecycle rules, visit GitHub.

  • For more information about the API operation that you can call to configure lifecycle rules, see PutBucketLifecycle.

  • For more information about the API operation that you can call to query lifecycle rules, see GetBucketLifecycle.