• Object Storage Service

obs
  1. Help Center
  2. Object Storage Service
  3. User Guide (S3cmd)
  4. Operation Guide
  5. Bucket Management
  6. Configuring a Lifecycle Rule

Configuring a Lifecycle Rule

Bucket owners or users who have the following permissions on the buckets can configure a lifecycle rule. Specify a lifecycle rule to automatically delete objects that match the rule once they expire, saving your storage space and costs.

  • s3:PutLifecycleConfiguration
  • s3:GetLifecycleConfiguration
NOTE:

You can obtain these permissions by configuring a bucket policy. For details about how to configure a bucket policy, see section Configuring a Bucket Policy.

Procedure

  1. Open the command line tool.
  2. Run the following command to create a file that stores a lifecycle rule:

    vi lifecycle-file-name

    NOTE:

    lifecycle-file-name is the name of the file that stored a lifecycle rule and can be user-defined.

  3. Press i to go to the edit mode and configure a lifecycle rule in the following format:

    <LifecycleConfiguration>
      <Rule>
         <ID>id</ID>
         <Prefix>prefix</Prefix>
         <Status>status</Status>
         <Expiration>
            <Days>days</Days>
         </Expiration>
         <NoncurrentVersionExpiration>
            <NoncurrentDays>1</NoncurrentDays>
         </NoncurrentVersionExpiration>
      </Rule>
    </LifecycleConfiguration>

    Table 1 describes parameters in a lifecycle rule.

    Table 1 Parameters of a lifecycle rule

    Parameter

    Description

    Mandatory or Optional

    LifecycleConfiguration

    Indicates the container for lifecycle rules. You can add multiple rules. The total size of the rules cannot exceed 20 KB.

    Type: XML

    Children: Rule

    Ancestor: None

    Mandatory

    Rule

    Indicates the container for a specific lifecycle rule.

    Type: Container

    Ancestor: LifecycleConfiguration

    Mandatory

    ID

    Indicates the unique identifier of a rule. The value can contain a maximum of 255 characters.

    Type: String

    Ancestor: Rule

    Optional

    Prefix

    Indicates the object name prefix identifying one or more objects to which the rule applies.

    Type: String

    Ancestor: Rule

    Mandatory

    Status

    Indicates whether the rule is enabled.

    Type: String

    Ancestor: Rule

    Valid values: Enabled and Disabled, which indicate that the rule is enabled or disabled respectively.

    Mandatory

    Expiration

    Indicates the container for the object expiration rule (only applicable to the latest versions of objects).

    Type: XML

    Children: Days

    Ancestor: Rule

    Mandatory

    Days

    Indicates the number of days when the rule takes effect after the object creation (only applicable to latest versions of objects).

    Type: Integer

    Ancestor: Expiration

    Mandatory

    NoncurrentVersionExpiration

    Indicates the container for the expiration time of objects' historical versions. If versioning of a bucket is enabled or suspended, you can set NoncurrentVersionExpiration to delete historical versions of objects that match the lifecycle rule (only applicable to the historical versions of objects).

    Type: XML

    Children: NoncurrentDays

    Ancestor: Rule

    Optional

    NoncurrentDays

    Indicates the number of days when the specified rule takes effect after the object becomes a historical version (only applicable to an object's historical version).

    Type: Integer

    Ancestor: NoncurrentVersionExpiration

    Mandatory if NoncurrentVersionExpiration exists

  4. Press Esc, input :wq!, and press Enter to save and exit the vi editor.
  5. Run the following command to configure a lifecycle rule for a specific bucket:

    s3cmd setlifecycle lifecycle-file-name s3://my-bucket

    NOTE:

    lifecycle-file-name is the name of the file that stores a lifecycle rule and my-bucket is the name of the bucket for which you want to configure this lifecycle rule.

    If the following information is displayed in the command output, it indicates that this lifecycle rule is successfully configured:
    s3://my-bucket/: Lifecycle Policy updated

  6. (Optional) Run the following command to view the basic information about the bucket and check that the lifecycle rule is configured:

    s3cmd info s3://my-bucket

    NOTE:

    Bucket owners or users who have all the following five permissions can perform this operation.

    • s3:GetBucketLocation
    • s3:GetLifecycleConfiguration
    • s3:GetBucketPolicy
    • s3:GetBucketCORS
    • s3:GetBucketAcl

    You can obtain these permissions by configuring a bucket policy. For details about how to configure a bucket policy, see section Configuring a Bucket Policy.

Example

The following provides a configuration example. This example introduces how to use a lifecycle rule to manage log files that are periodically uploaded.

Some periodically uploaded files need only to be retained for one week or one month, and can be deleted once they expire. In the following configuration example, log files whose name prefixes are logs/ will be automatically deleted after they are retained on OBS for 7 days.

<LifecycleConfiguration>
  <Rule>
     <ID>delete-7-days</ID>
     <Prefix>logs/</Prefix>
     <Status>Enabled</Status>
     <Expiration>
        <Days>7</Days>
     </Expiration>
  </Rule>
</LifecycleConfiguration>

Follow-up Procedure

You can delete the lifecycle rule of a specific bucket if you do not need automatic object deletion for this bucket. The deletion procedure is as follows:

  1. Open the command line tool.
  2. Run the following command to delete the lifecycle rule for a specific bucket:

    s3cmd dellifecycle s3://my-bucket

  3. If the following information is displayed in the command output, it indicates that this lifecycle rule is successfully deleted:

    s3://my-bucket/: Lifecycle Policy deleted

  4. (Optional) Run the following command to view the basic information about the bucket and check that the lifecycle rule is successfully deleted:

    s3cmd info s3://my-bucket

    NOTE:

    Bucket owners or users who have all the following five permissions can perform this operation.

    • s3:GetBucketLocation
    • s3:GetLifecycleConfiguration
    • s3:GetBucketPolicy
    • s3:GetBucketCORS
    • s3:GetBucketAcl

    You can obtain these permissions by configuring a bucket policy. For details about how to configure a bucket policy, see section Configuring a Bucket Policy.