All Products
Search
Document Center

Object Storage Service:put-bucket-lifecycle

Last Updated:Jul 31, 2024

Creates lifecycle rules based on the last modified time or last access time of objects to periodically move objects to the specified storage class or periodically delete expired objects and parts for cost optimization.

Usage notes

  • To configure a lifecycle rule for a bucket, you must have the oss:PutBucketLifecycle permission. For more information, see Attach a custom policy to a RAM user.

  • If no lifecycle rules are configured for the bucket, the command creates a lifecycle rule for the bucket. If the bucket has existing lifecycle rules, the command overwrites the existing lifecycle rules with new rule configuration.

  • You can use lifecycle rules to specify the expiration time of objects and parts that are uploaded in incomplete multipart upload tasks.

Command syntax

ossutil api put-bucket-lifecycle --bucket value --lifecycle-configuration value [flags]

Parameter

Type

Description

--bucket

string

The name of the bucket.

--lifecycle-configuration

string

The container for lifecycle rules. A lifecycle configuration container can contain up to 1,000 rules.

Note

The put-bucket-lifecycle command is the equivalent of the PutBucketLifecycle operation. For more information about the API operation, see PutBucketLifecycle.

--lifecycle-configuration

The --lifecycle-configuration option supports XML and JSON formats.

  • XML format:

    <LifecycleConfiguration>
      <Rule>
        <NoncurrentVersionExpiration>
          <NoncurrentDays>integer</NoncurrentDays>
        </NoncurrentVersionExpiration>
        <NoncurrentVersionTransition>
          <NoncurrentDays>integer</NoncurrentDays>
          <StorageClass>string</StorageClass>
          <IsAccessTime>boolean</IsAccessTime>
          <ReturnToStdWhenVisit>boolean</ReturnToStdWhenVisit>
          <AllowSmallFile>boolean</AllowSmallFile>
        </NoncurrentVersionTransition>
        ...
        <ID>string</ID>
        <Prefix>string</Prefix>
        <Status>string</Status>
        <Expiration>
          <ExpiredObjectDeleteMarker>boolean</ExpiredObjectDeleteMarker>
          <CreatedBeforeDate>string</CreatedBeforeDate>
          <Days>integer</Days>
        </Expiration>
        <Transition>
          <IsAccessTime>boolean</IsAccessTime>
          <ReturnToStdWhenVisit>boolean</ReturnToStdWhenVisit>
          <AllowSmallFile>boolean</AllowSmallFile>
          <CreatedBeforeDate>string</CreatedBeforeDate>
          <Days>integer</Days>
          <StorageClass>string</StorageClass>
        </Transition>
        ...
        <AbortMultipartUpload>
          <CreatedBeforeDate>string</CreatedBeforeDate>
          <Days>integer</Days>
        </AbortMultipartUpload>
        <Tag>
          <Key>string</Key>
          <Value>string</Value>
        </Tag>
        ...
        <Filter>
          <Not>
            <Prefix>string</Prefix>
            <Tag>
              <Key>string</Key>
              <Value>string</Value>
            </Tag>
          </Not>
        </Filter>
      </Rule>
      ...
    </LifecycleConfiguration>
  • JSON format:

    {
      "Rule": [
        {
          "Tag": [
            {
              "Key": "string",
              "Value": "string"
            },
            ...
          ],
          "Filter": {
            "Not": {
              "Prefix": "string",
              "Tag": {
                "Key": "string",
                "Value": "string"
              }
            }
          },
          "Transition": [
            {
              "CreatedBeforeDate": "string",
              "Days": integer,
              "StorageClass": "string",
              "IsAccessTime": boolean,
              "ReturnToStdWhenVisit": boolean,
              "AllowSmallFile": boolean
            },
            ...
          ],
          "AbortMultipartUpload": {
            "CreatedBeforeDate": "string",
            "Days": integer
          },
          "NoncurrentVersionExpiration": {
            "NoncurrentDays": integer
          },
          "NoncurrentVersionTransition": [
            {
              "NoncurrentDays": integer,
              "StorageClass": "string",
              "IsAccessTime": boolean,
              "ReturnToStdWhenVisit": boolean,
              "AllowSmallFile": boolean
            },
            ...
          ],
          "ID": "string",
          "Prefix": "string",
          "Status": "string",
          "Expiration": {
            "CreatedBeforeDate": "string",
            "Days": integer,
            "ExpiredObjectDeleteMarker": boolean
          }
        },
        ...
      ]
    }
Note

For more information, see Command-line options.

Examples

Create a lifecycle rule for the examplebucket bucket.

  • Use an XML configuration file (lifecycle-configuration.xml in this example)

    <?xml version="1.0" encoding="UTF-8"?>
    <LifecycleConfiguration>
            <Rule>
                    <ID>rule1</ID>
                            <Prefix>tmp/</Prefix>
                            <Status>Enabled</Status>
                            <Expiration>
                                    <Days>10</Days>
                            </Expiration>
                            <Transition>
                                    <Days>5</Days>
                                    <StorageClass>IA</StorageClass>
                            </Transition>
                            <AbortMultipartUpload>
                            <Days>10</Days>
                    </AbortMultipartUpload>
            </Rule>
    </LifecycleConfiguration>

    Sample command:

    ossutil api put-bucket-lifecycle --bucket examplebucket --lifecycle-configuration file://lifecycle-configuration.xml
  • Use a JSON configuration file (lifecycle-configuration.json in this example)

    {
            "Rule": {
                    "ID": "rule1",
                    "Prefix": "tmp/",
                    "Status": "Enabled",
                    "Expiration": {
                            "Days": "10"
                    },
                    "Transition": {
                            "Days": "5",
                            "StorageClass": "IA"
                    },
                    "AbortMultipartUpload": {
                            "Days": "10"
                    }
            }
    }

    Sample command:

    ossutil api put-bucket-lifecycle --bucket examplebucket --lifecycle-configuration file://lifecycle-configuration.json
  • Use JSON parameters in the command line

    ossutil api put-bucket-lifecycle --bucket examplebucket --lifecycle-configuration "{\"Rule\":{\"ID\":\"rule1\",\"Prefix\":\"tmp/\",\"Status\":\"Enabled\",\"Expiration\":{\"Days\":\"10\"},\"Transition\":{\"Days\":\"5\",\"StorageClass\":\"IA\"},\"AbortMultipartUpload\":{\"Days\":\"10\"}}}"