This topic describes how to manage lifecycle rules.

You can configure lifecycle rules for OSS buckets to reduce storage costs. The lifecycle rule allows OSS to automatically delete expired objects or parts of an object. It also converts the storage class of expired objects to IA or Archive. A lifecycle rule includes the following fields:
  • Rule ID. It identifies a rule. Ensure that each rule ID in a bucket is unique.
  • Policy. You can configure a policy by using the following configuration methods. Only one method can be configured for a bucket.
    • Configure by Prefix: You can create multiple rules by using this method. Ensure that each prefix is unique.
    • Configure for the entire bucket: You can configure only one rule by using this method.
  • Expiration. You can configure the time when an object expires in the following methods:
    • Configure by number of days: specifies a number of days. Objects expire the specified days after they are last modified.
    • Configure by date: specifies a date. Objects expire if they are last modified before the specified date.
  • Whether this rule takes effect.

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

For more information about lifecycle rules, see Manage object lifecycle. For the complete code used to manage lifecycle rules, visit GitHub.

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

    // The first rule (id:"rule1", enable:true, prefix:"foo/", expiry:Days 3) indicates that an object prefixed with foo expires three days after it is last modified.
    rule1 := oss.BuildLifecycleRuleByDays("rule1", "foo/", true, 3)

    // If a versioning-enabled object is a delete marker and there are no other versions, the delete marker is deleted.
    deleteMark := true
    expiration := oss.LifecycleExpiration{
        ExpiredObjectDeleteMarker: &deleteMark,
    }

    // Configure the lifecycle rule to delete the object 30 days after the object becomes the historical version.
    versionExpiration := oss.LifecycleVersionExpiration{
        NoncurrentDays: 30,
    }

    // Configure the lifecycle rule to convert the storage class of the object to IA 10 days after the object becomes a historical version.
    versionTransition := oss.LifecycleVersionTransition{
        NoncurrentDays: 10,
        StorageClass: "IA",
    }

    // Configure the second rule.
    rule2 := oss.LifecycleRule{
        ID:                   "rule2",
        Prefix:               "<yourObjectPrefix>",
        Status:               "Enabled",
        Expiration:           &expiration,
        NonVersionExpiration: &versionExpiration,
        NonVersionTransition: &versionTransition,
    }

    //The third rule indicates that the objects whose tag consists of keyA and valueA expire three days after it is 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 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)
    }

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

Clear lifecycle rules

The following code provides an example on how to clear 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)
    }

    bucketName := "<yourBucketName>"

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