OSS allows you to configure lifecycle rules to delete expired objects and parts or convert the storage class of expired objects to IA or Archive. This way, storage costs are minimized. This topic describes how to manage lifecycle rules.

Background information

Each lifecycle rule contains the following information:

  • Policy: specifies the mode to match objects and parts.
    • Match by prefix: matches objects and parts by prefix. You can create multiple rules to configure different prefixes. Each prefix must be unique.
    • Match by tag: matches objects by tag key and tag value. You can configure multiple tags for a single rule. OSS runs lifecycle rules for all objects that have these tags. Tags cannot be configured for parts to match lifecycle rules.
      Note For more information, see Object tagging.
    • Match by prefix and tag: matches objects by specifying a prefix and one or more tags.
    • Match by bucket: matches all objects and parts contained in the bucket. If you select this method, only a single rule can be created.
  • Object lifecycle policy: specifies the validity period or expiration date and the operation to perform on these objects when they expire.
    • Validity period: specifies the number of days for unversioned and current objects and the operation to perform on these objects. Objects that meet the specified conditions are retained for the specified period of time after the objects are last modified. The specified operation is performed on these objects when they expire.
    • Expiration date: specifies a date for unversioned and current objects and the operation to perform on these objects. All objects that are last modified before this date expire and the specified operation is performed on these objects.
    • Validity period for the previous versions of objects: specifies the number of days for the previous versions of objects and the operation to perform on these objects. Objects that meet the specified conditions are retained for the specified period of time after the objects become previous versions. The specified operation is performed on these objects when they expire.
    Note You can specify that the storage class of objects is converted to IA or Archive, or delete the objects when they expire. For more information, see Configuration elements.
  • Part lifecycle policy: specifies the validity period or expiration date and the operation to perform on these parts when they expire.
    • Validity period: specifies the number of days for which to retain parts after they are last modified and the delete operation to perform on these parts when they expire.
    • Expiration date: specifies a date based on which to delete parts. Parts that are last modified before this date expire.

You can also configure lifecycle rules for parts that are uploaded to a bucket using uploadPart. The last modification time uses the time the multipart upload task is initiated.

For more information, see Manage object lifecycles.

Configure lifecycle rules

The following code provides an example on how to configure lifecycle rules:

package main

import (
    "fmt"
    "os"

    "github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
    // Create an OSSClient instance.
    client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
    if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Create a lifecycle rule and set id to rule1, enable to true, prefix to foo/, and expiry:Days to 3. This rule applies to objects whose names are prefixed with fool. These objects expire three days after they are last modified.
    rule1 := oss.BuildLifecycleRuleByDays("rule1", "foo/", true, 3)

    // If an object in a versioned bucket is a delete marker and has no other versions, the delete marker is removed.
    deleteMark := true
    expiration := oss.LifecycleExpiration{
        ExpiredObjectDeleteMarker: &deleteMark,
    }

    // Specify that objects are deleted when they expire 30 days after they become previous versions.
    versionExpiration := oss.LifecycleVersionExpiration{
        NoncurrentDays: 30,
    }

    // Specify that the storage class of objects is converted to IA 10 days after they become previous versions.
    versionTransition := oss.LifecycleVersionTransition{
        NoncurrentDays: 10,
        StorageClass:   "IA",
    }

    // Create a lifecycle rule and set id to rule2.
    rule2 := oss.LifecycleRule{
        ID:                   "rule2",
        Prefix:               "<yourObjectPrefix>",
        Status:               "Enabled",
        Expiration:           &expiration,
        NonVersionExpiration: &versionExpiration,
        NonVersionTransition: &versionTransition,
    }

    // Create a lifecycle rule and set id to rule3. This rule applies to objects that have the tag with the tag key of tagA and tag value of A. These objects expire three days after they are last modified.
    rule3 := oss.LifecycleRule{
        ID:     "rule3",
        Prefix: "",
        Status: "Enabled",
        Tags: []oss.Tag{
            oss.Tag{
                Key:   "tagA",
                Value: "A",
            },
        },
        Expiration: &oss.LifecycleExpiration{Days: 3},
    }

    // Configure the lifecycle rules.
    rules := []oss.LifecycleRule{rule1, rule2, rule3}
    bucketName := "<yourBucketName>"
    err = client.SetBucketLifecycle(bucketName, rules)
    if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
}

View lifecycle rules

The following code provides an example on how to view the lifecycle rules configured for a bucket:

package main

import (
    "fmt"
    "os"
    "github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
    // Create an OSSClient instance.
    client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
    if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    bucketName := "<yourBucketName>"

    // View the lifecycle rules.
    lcRes, err := client.GetBucketLifecycle(bucketName)
    if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    fmt.Println("Lifecycle Rules:", lcRes.Rules)
}
			

Delete lifecycle rules

The following code provides an example on how to delete the lifecycle rules configured for a bucket:

package main

import (
    "fmt"
    "os"
    "github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
    // Create an OSSClient instance.
    client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
    if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    bucketName := "<yourBucketName>"

    // Delete the lifecycle rules.
    err = client.DeleteBucketLifecycle(bucketName)
    if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }