Some data uploaded to Object Storage Service (OSS) may not be frequently accessed but still needs to be stored in cold storage due to compliance or archiving requirements. In some business scenarios, you may want to automatically delete data that is no longer needed. To achieve this goal, you can run the lifecycle command to create a lifecycle rule based on the last modified time of the data. You can configure lifecycle rules based on the last access time of data stored in a bucket to implement tiered storage of hot and cold data and reduce storage costs. After you configure such a lifecycle rule, OSS monitors the access patterns of the objects in the bucket, identifies cold data based on access patterns, and automatically moves cold data to the specified storage class.
Usage notes
To create or modify a lifecycle rule, you must have the
oss:PutBucketLifecycle
permission. To query a lifecycle rule, you must have theoss:GetBucketLifecycle
permission. To delete a lifecycle rule, you must have theoss:DeleteBucketLifecycle
permission. For more information, see Attach a custom policy to a RAM user.Before you configure lifecycle rules based on the last modified time or last access time of objects, make sure that you familiarize yourself with this feature. For more information, see Lifecycle rules based on the last modified time and Lifecycle rules based on the last access time.
For ossutil 1.6.16 and later, you can directly use ossutil as the binary name in the command line. You do not need to update the binary name based on the operating system. For ossutil earlier than 1.6.16, you need to update the binary name based on the operating system. For more information, see ossutil command reference.
For more information about lifecycle rules, see Overview.
You can use a lifecycle rule to delete objects from a CloudBox bucket. You cannot use a lifecycle rule to change the storage class of objects in a CloudBox bucket.
Create or modify lifecycle rules
You can perform the following steps to create or modify lifecycle rules:
Create a local file and configure lifecycle rules in the XML format in the file.
Use ossutil to read the lifecycle configuration from the local file, and then add the configuration to the specified bucket.
Command syntax
ossutil lifecycle --method put oss://bucketname local_xml_file
The following table describes the parameters in the command syntax.
Parameter
Description
bucketname
The name of the bucket for which you want to create or modify lifecycle rules.
local_xml_file
The name of the local file in which the lifecycle rules are configured. Example:
localfile.xml
.Examples
ImportantYou can create multiple lifecycle rules for a bucket. Each rule is identified by a unique ID. If the ID of the lifecycle rule that you want to create is the same as that of an existing lifecycle rule, HTTP status code 409 is returned.
Create a file named
localfile.xml
on your local computer and write lifecycle rules into the file.Sample lifecycle configurations:
Example 1
The following lifecycle configuration for the examplebucket bucket contains two lifecycle rules. The first lifecycle rule applies to all objects (with Prefix set to an empty value) in the bucket and deletes matching objects 365 days after the objects are last modified. The second lifecycle rule moves objects whose names contain the "test/" prefix to the Archive storage class 30 days after the objects are last modified.
For information about the storage classes that are supported by lifecycle rules, see Configure lifecycle rules to automatically convert the storage classes of objects.
<?xml version="1.0" encoding="UTF-8"?> <LifecycleConfiguration> <Rule> <ID>test-rule1</ID> <Prefix></Prefix> <Status>Enabled</Status> <Expiration> <Days>365</Days> </Expiration> </Rule> <Rule> <ID>test-rule2</ID> <Prefix>test/</Prefix> <Status>Enabled</Status> <Transition> <Days>30</Days> <StorageClass>Archive</StorageClass> </Transition> </Rule> </LifecycleConfiguration>
Example 2
The following lifecycle rule applies to all objects in the examplebucket bucket (with Prefix set to an empty value) and expires matching objects whose last modified time is earlier than December 30, 2019:
<?xml version="1.0" encoding="UTF-8"?> <LifecycleConfiguration> <Rule> <ID>test-rule0</ID> <Prefix></Prefix> <Status>Enabled</Status> <Expiration> <CreatedBeforeDate>2019-12-30T00:00:00.000Z</CreatedBeforeDate> </Expiration> </Rule> </LifecycleConfiguration>
Example 3
The following lifecycle rule specifies that objects in versioning-enabled bucket examplebucket are moved to Infrequent Access (IA) 10 days after the objects are last modified and that noncurrent versions are moved to Archive 60 days after they become noncurrent versions and deleted 90 days after they become noncurrent versions:
<?xml version="1.0" encoding="UTF-8"?> <LifecycleConfiguration> <Rule> <ID>test-rule3</ID> <Prefix></Prefix> <Status>Enabled</Status> <Transition> <Days>10</Days> <StorageClass>IA</StorageClass> </Transition> <NoncurrentVersionTransition> <NoncurrentDays>60</NoncurrentDays> <StorageClass>Archive</StorageClass> </NoncurrentVersionTransition> <NoncurrentVersionExpiration> <NoncurrentDays>90</NoncurrentDays> </NoncurrentVersionExpiration> </Rule> </LifecycleConfiguration>
Example 4
The following lifecycle rule specifies that objects whose names contain the data/ prefix are moved to IA 200 days after the objects are last accessed and remain in IA after they are accessed again.
<?xml version="1.0" encoding="UTF-8"?> <LifecycleConfiguration> <Rule> <ID>test-rule4</ID> <Prefix>data/</Prefix> <Status>Enabled</Status> <Transition> <Days>200</Days> <StorageClass>IA</StorageClass> <IsAccessTime>true</IsAccessTime> <ReturnToStdWhenVisit>false</ReturnToStdWhenVisit> </Transition> </Rule> </LifecycleConfiguration>
Run the following command to add the lifecycle configuration to the examplebucket bucket:
ossutil lifecycle --method put oss://examplebucket localfile.xml
The following sample output indicates that the lifecycle configuration is added.
0.299514(s) elapsed
Query lifecycle rules
Command syntax
ossutil lifecycle --method get oss://bucketname [local_xml_file]
The following table describes the parameters in the command syntax.
Parameter
Description
bucketname
The name of the bucket whose lifecycle rules you want to query.
local_xml_file
The name of the local file in which you want to store the lifecycle configuration. Example:
localfile.xml
. If you do not configure this parameter, the lifecycle rules are directly displayed on your screen.Examples
Run the following command to query the lifecycle rules of the examplebucket bucket:
ossutil lifecycle --method get oss://examplebucket localfile.xml
The following sample output indicates that the lifecycle configuration is queried and written to the localfile.xml file:
0.212407(s) elapsed
Delete lifecycle rules
Command syntax
ossutil lifecycle --method delete oss://bucketname
Examples
Run the following command to delete the lifecycle rules of the examplebucket bucket:
ossutil lifecycle --method delete oss://examplebucket
The following sample output indicates that the lifecycle rules of the examplebucket bucket are deleted.
0.530750(s) elapsed
Common options
If you use ossutil to switch to a bucket that is located in another region, add the -e option to specify the endpoint of the region in which the bucket is located. If you use ossutil to switch to a bucket that belongs to another Alibaba Cloud account, add the -i option to specify the AccessKey ID of the specified account, and add the -k option to specify the AccessKey secret of the specified account.
For example, you can run the following command to create lifecycle rules for a bucket named examplebucket, which is located in the China (Hangzhou) region and is owned by another Alibaba Cloud account:
ossutil lifecycle --method put oss://examplebucket localfile.xml -e oss-cn-hangzhou.aliyuncs.com -i LTAI4Fw2NbDUCV8zYUzA**** -k 67DLVBkH7EamOjy2W5RVAHUY9H****
For more information about common options, see Common options.