All Products
Search
Document Center

Object Storage Service:Configure lifecycle rules

Last Updated:Jan 11, 2024

Not all data that is uploaded to Object Storage Service (OSS) buckets is frequently accessed. Rarely accessed data is stored in cold storage for compliance and archiving purposes. If you want to delete data stored in multiple OSS buckets at the same time, you can run the ossutil lifecycle command to configure lifecycle rules based on the last modified time of the data. To reduce storage costs, we recommend that you store cold data and hot data separately. To achieve separate storage of hot data and cold data, you can configure OSS to automatically monitor data access modes, identify cold data, and then change the storage class of the cold data. In this scenario, you can also configure lifecycle rules based on the last access time of the data.

Usage notes

  • Sample command lines in this topic are based on the 64-bit Linux system. For other systems, replace ./ossutil64 in the commands with the corresponding binary name. For more information, see ossutil command reference.

  • For more information about lifecycle rules, see Overview.

  • OSS allows you to delete objects from CloudBox buckets only by using lifecycle rules. You cannot change the storage classes of objects.

Add or modify lifecycle rules

You can perform the following steps to add or modify lifecycle rules:

  1. Create a local file and configure lifecycle rules in the XML format in the file.

  2. Use ossutil to read the lifecycle configurations from the local file, and then add the configurations to the specified bucket.

  • Command syntax

    ./ossutil64 lifecycle --method put oss://bucketname local_xml_file

    The following table describes the parameters in the preceding command.

    Parameter

    Description

    bucketname

    The name of the bucket for which you want to add or modify lifecycle rules.

    local_xml_file

    The name of the local file in which the lifecycle rules are configured. Example: localfile.xml.

  • Examples

    Important

    You can add 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 add is the same as that of an existing lifecycle rule, HTTP status code 409 is returned.

    1. Create a file named localfile.xml on your local computer and write lifecycle rules into the file based on your business requirements.

      Sample lifecycle rules:

      • Example 1

        The following code provides examples of two lifecycle rules. This first rule takes effect on all objects in the examplebucket bucket. This indicates that the Prefix field in the command is left empty. The objects that match the first rule are deleted 365 days after the objects are last modified. The second rule takes effect on objects prefixed with test/. This indicates that the Prefix field in the command is set to test/. The storage classes of the objects that match the second rule are changed to Archive 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 code provides an example of a lifecycle rule. The lifecycle rule takes effect on all objects in the examplebucket bucket. This indicates that the Prefix field in the command is left empty. This rule specifies that objects whose last modified time is earlier than December 30, 20129 expire.

        <?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 code provides an example of a lifecycle rule. The lifecycle rule takes effect on versioning-enabled objects in the examplebucket bucket. This rule specifies that the storage classes of objects are changed to Infrequent Access (IA) 10 days after the objects are last modified, the storage classes of the previous versions of objects are changed to Archive 60 days after the objects are last modified, and the previous versions of objects are deleted 90 days after the objects are last modified.

        <?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 code provides an example of a lifecycle rule. The lifecycle rule takes effect on objects prefixed with data/. The rule specifies that the storage classes of objects prefixed with data/ are changed to IA 200 days after the objects are last accessed. The rule also specifies that the storage classes of objects prefixed with logs are still IA if the objects 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>

  1. Run the following command to add the lifecycle rules to the examplebucket bucket:

    ./ossutil64 lifecycle --method put oss://examplebucket localfile.xml

    If an output similar to the following code is displayed, the lifecycle rules are added.

    0.299514(s) elapsed

Query lifecycle rules

  • Command syntax

    ./ossutil64 lifecycle --method get oss://bucketname [local_xml_file]

    The following table describes the parameters in the preceding command.

    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 the lifecycle rules are stored. 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:

    ./ossutil64 lifecycle --method get oss://examplebucket localfile.xml

    If an output similar to the following code is displayed, the lifecycle rules of the examplebucket bucket are obtained and written to the localfile.xml file.

    0.212407(s) elapsed

Delete lifecycle rules

  • Command syntax

    ./ossutil64 lifecycle --method delete oss://bucketname
  • Examples

    Run the following command to delete the lifecycle rules of the examplebucket bucket:

    ./ossutil64 lifecycle --method delete oss://examplebucket

    If an output similar to the following code is displayed, the lifecycle rules of the localfile.xml 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 the command to specify the endpoint of the region in which the specified bucket is located. If you use ossutil to switch to a bucket that belongs to another Alibaba Cloud account, you can add the -i option to the command to specify the AccessKey ID of the specified account, and add the -k option to the command to specify the AccessKey secret of the specified account.

For example, you can run the following command to configure lifecycle rules for a bucket named examplebucket, which is located in the China (Hangzhou) region and is owned by another Alibaba Cloud account.

./ossutil64 lifecycle --method put oss://examplebucket localfile.xml -e oss-cn-hangzhou.aliyuncs.com -i LTAI4Fw2NbDUCV8zYUzA****  -k 67DLVBkH7EamOjy2W5RVAHUY9H****

For more information about other common options that you can use for the sync command, see View options.