Object Storage Service (OSS) allows you to configure lifecycle rules to delete expired objects and parts or convert the storage class of expired objects to Infrequent Access (IA) or Archive. This way, storage costs are minimized. This topic describes how to manage lifecycle rules for buckets.

Background information

Each lifecycle rule contains the following information:

  • The match policy of prefixes or tags: specifies the objects and parts that match the rule.
    • Match by prefix: specifies that the rule matches objects and parts by prefix. You can create multiple rules to match objects whose names contain different prefixes. You can only configure a lifecycle rule for each prefix.
    • Match by tag: matches objects by tag key and tag value. You can specify multiple tags in a single lifecycle rule. The lifecycle rule applies to all objects that have the specified tags. Lifecycle rules cannot be configured for parts to match tags.
      Note For more information about object tags, see Configure 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 a bucket. In this case, you can create only one lifecycle rule for the bucket.
  • Object lifecycle policy: specifies the validity period or the expiration date of objects and the operation to perform on the expired objects.
    • Validity period: specifies the validity period of all objects in unversioned buckets and the current versions of objects in versioned buckets and the operation to perform on these objects after they expire. Objects that match the lifecycle rule are retained for the specified validity period after the objects are last modified. The specified operation is performed on these objects after they expire.
    • Expiration date: specifies the expiration date of all objects in unversioned buckets and the current versions of objects in versioned buckets and the operation to perform on these objects after they expire. 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 validity period for the previous versions of objects and the operation to perform on these previous versions. Objects that match the lifecycle rule are retained for the specified validity period after the objects become previous versions. The specified operation is performed on these objects after they expire.
    Note You can specify that the storage class of expired objects is converted to IA or Archive, or delete the expired objects. For more information, see Configuration elements.
  • Part lifecycle policy: specifies the validity period or the expiration date of parts and the operation to perform after the parts expire.
    • Validity period: specifies the validity period for parts. Parts that match the lifecycle rule are retained within the validity period and are deleted after they expire.
    • Expiration date: specifies the expiration date of parts. Parts that are last modified before this date expire and are deleted.

Lifecycle rules also apply to the parts uploaded by using uploadPart. In this case, the last modified time of an object is the time the multipart upload task is initiated.

For more information about lifecycle rules, see Lifecycle rules.

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 query the lifecycle rules configured for the bucket named examplebucket:

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 clear lifecycle rules of a bucket named examplebucket:

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)
    }