After you configure lifecycle rules for a bucket, OSS (Object Storage Service) regularly converts the storage class of objects in the bucket to Infrequent Access (IA), Archive, or Cold Archive, or deletes expired objects and parts to save your storage costs. This topic describes how to run the lifecycle command to add, modify, query, and delete lifecycle rules.

Note
  • Sample command lines in this topic are based on the 64-bit Linux system. For other systems, replace the ./ossutil64 part in the commands with the corresponding binary name. For more information, see ossutil.
  • For more information about lifecycle rules, see Lifecycle rules based on the last modified time.

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. ossutil reads the lifecycle rule configurations from the local file, and then adds the configurations to the specified bucket.
  • Command syntax
    ./ossutil64 lifecycle --method put oss://bucketname local_xml_file

    The following table describes the parameters that you can configure when you run this command to add or modify lifecycle rules.

    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 rule is configured. Example: localfile.txt.
  • Examples
    Notice You can add multiple lifecycle rules for a bucket. Each rule is identified by their unique IDs. If the ID of the lifecycle rule you want to add already exists, the HTTP status code 409 is returned.
    1. Create a file named localfile.txt on your local computer and configure lifecycle rules based on your requirements.

      The following examples show how to configure some common lifecycle rules:

      • Example 1

        Specify that objects in the bucket named examplebucket are deleted 365 days after they are last modified, and the storage class of objects whose names contain the test/ prefix is converted to Archive 30 days after they are last modified.

        For more information about how to convert the storage class of objects by using lifecycle rules, see Configure lifecycle rules to automatically convert the storage class of an object.

        <?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

        Specify that objects that are last modified before December 30, 2019 in the bucket named examplebucket 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

        Specify that the storage class of objects in the versioning-enabled bucket named examplebucket is converted to IA 10 days after they are last modified. In addition, the storage class of objects is converted to Archive 60 days after they become a previous version, and objects are deleted 90 days after they become a previous version.

        <?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>
    2. Add lifecycle rules for the bucket named examplebucket.
      ./ossutil64 lifecycle --method put oss://examplebucket localfile.txt

      If a similar output is displayed, the lifecycle rules are added to the bucket:

      0.299514(s) elapsed

Query lifecycle rules

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

    The following table describes the parameters that you can configure when you run this command to query lifecycle rules configured for a bucket.

    Parameter Description
    bucketname The name of the bucket whose lifecycle rules you want to query.
    local_xml_file The name of the local file used to store obtained lifecycle rule configurations. Example: localfile.txt. If this parameter is not specified, the obtained lifecycle rule configurations are displayed on the screen.
  • Examples

    You can run the following command to query the lifecycle rules configured for the bucket named examplebucket:

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

    If a similar output is displayed, the lifecycle rules configured for the bucket named examplebucket are obtained and written to the localfile.txt file:

    0.212407(s) elapsed

Delete lifecycle rules

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

    You can run the following command to delete the lifecycle rules configured for the bucket named examplebucket:

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

    If a similar output is displayed, the lifecycle rules configured for the bucket named examplebucket are deleted:

    0.530750(s) elapsed

Common options

If you use ossutil to switch to a bucket that is located in a different region, you can use the -e option to specify the endpoint of the region in which the specified bucket resides. If you use ossutil to switch to a bucket that belongs to another Alibaba Cloud account, you can use the -i option to specify the AccessKey ID of the specified account, and use the -k option 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.txt -e oss-cn-hangzhou.aliyuncs.com -i LTAI4Fw2NbDUCV8zYUzA****  -k 67DLVBkH7EamOjy2W5RVAHUY9H****

For more information about other common options that apply to the lifecycle command, see Common options.