You can use OSS to configure lifecycle rules to reduce storage costs. The lifecycle management rules can be used for automatic deletion of expired objects and fragments, or storage class conversion to Infrequent Access for objects that will expire soon. Each rule includes:

  • 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 with this method. Ensure that each prefix is unique.
    • Configure for Entire Bucket: You can configure only one rule with this method.
  • Expired: You can configure the following configuration methods:
    • Set by Number of Days: It specifies the number of days from the last modification time of objects.
    • Set by Date: It specifies the date prior to which objects are created. If objects are created prior to the date, these objects are deleted.
  • Status: This lifecyle rule takes effect or not.

The parts uploaded by uploadPart method also support lifecycle rules. The last modification time of an object is the time the multipart upload event is initiated.

For more information about lifecycle management, see Manage object lifecycle. For the complete code of lifecycle management, see GitHub.

Configure lifecycle rules

Run the following code 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)
	}

	// Lifecycle rule 1 (id: "rule1", enable:true, prefix:"foo/", expiry:Days 3), indicates that an object with the prefix foo expires three days after the last modified date.
	rule1 := oss.BuildLifecycleRuleByDays("rule1", "foo/", true, 3)
	rules := []oss.LifecycleRule{rule1}

	// Configure lifecycle rules.
	bucketName := "<yourBucketName>"
	err = client.SetBucketLifecycle(bucketName, rules)
	if err ! = nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
}

View lifecycle rules

Run the following code 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 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

Run the following code 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 lifecycle rules.
	err = client.DeleteBucketLifecycle(bucketName)
	if err ! = nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}