put-bucket-lifecycle

您可以基于最后一次修改时间以及最后一次访问时间的策略创建生命周期规则,定期将存储空间(Bucket)内的多个文件(Object)转储为指定存储类型,或者将过期的Object和碎片删除,从而节省存储费用。本文为您介绍如何使用put-bucket-lifecycle命令为存储空间(Bucket)设置生命周期规则。

注意事项

  • 要为存储空间设置生命周期规则,您必须有oss:PutBucketLifecycle权限。具体操作,请参见为RAM用户授权自定义的权限策略

  • 如果Bucket此前没有设置过生命周期规则,此操作会创建一个新的生命周期规则;如果Bucket此前设置过生命周期规则,此操作会覆写先前的规则配置。

  • 支持对Object以及Part(以分片方式上传,但最后未提交的分片)设置过期时间。

命令格式

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

参数

类型

说明

--bucket

string

Bucket名称。

--lifecycle-configuration

string

Lifecycle配置的容器,最多可容纳1000条规则。

说明

put-bucket-lifecycle命令对应API接口PutBucketLifecycle。关于API中的具体参数含义,请参见PutBucketLifecycle

--lifecycle-configuration

--lifecycle-configuration配置选项既支持XML语法也支持JSON语法:

  • XML语法:

    <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语法:

    {
      "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
          }
        },
        ...
      ]
    }
说明

关于支持的全局命令行选项,请参见支持的全局命令行选项

使用示例

为存储空间examplebucket设置生命周期规则。

  • 使用XML配置文件,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>

    命令示例如下:

    ossutil api put-bucket-lifecycle --bucket examplebucket --lifecycle-configuration file://lifecycle-configuration.xml
  • 使用JSON配置文件,lifecycle-configuration.json内容如下:

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

    命令示例如下:

    ossutil api put-bucket-lifecycle --bucket examplebucket --lifecycle-configuration file://lifecycle-configuration.json
  • 使用JSON配置参数,命令示例如下:

    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\"}}}"