You can create lifecycle rules based on the last modified time or last access time to periodically transition objects in a bucket to a specified storage class, or delete expired objects and parts to save storage costs. This topic describes how to use the put-bucket-lifecycle command to set lifecycle rules for a bucket.
Usage notes
To configure lifecycle rules for a bucket, you must have the
oss:PutBucketLifecyclepermission. For more information, see RAM Policy.If no lifecycle configuration is set for the bucket, this command creates a new one. If a lifecycle configuration already exists, this command overwrites it.
You can set an expiration time for objects and parts. Parts are uncommitted fragments from multipart uploads.
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. The configuration can contain up to 1,000 rules. |
--allow-same-action-overlap | string | Specifies whether to allow overlapping prefixes. Valid values:
|
The put-bucket-lifecycle command calls the PutBucketLifecycle API operation. For details about the API parameters, see PutBucketLifecycle.
--lifecycle-configuration
The --lifecycle-configuration option supports both XML and JSON syntax:
XML syntax:
<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 syntax:
{ "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 } }, ... ] }
For more information, see Command-line options.
Examples
Set lifecycle rules for the 'examplebucket' bucket.
Use an XML configuration file. The following shows the content of
lifecycle-configuration.xml:<?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>Command example:
ossutil api put-bucket-lifecycle --bucket examplebucket --lifecycle-configuration file://lifecycle-configuration.xmlUse a JSON configuration file. The following shows the content of
lifecycle-configuration.json:{ "Rule": { "ID": "rule1", "Prefix": "tmp/", "Status": "Enabled", "Expiration": { "Days": "10" }, "Transition": { "Days": "5", "StorageClass": "IA" }, "AbortMultipartUpload": { "Days": "10" } } }Command example:
ossutil api put-bucket-lifecycle --bucket examplebucket --lifecycle-configuration file://lifecycle-configuration.jsonUse a JSON string to specify the configuration directly in the command:
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\"}}}"