All Products
Search
Document Center

Object Storage Service:Lifecycle management (Go SDK V2)

Last Updated:Apr 03, 2026

This topic describes how to manage lifecycle rules for a bucket by using Go SDK V2.

Background

In OSS, not all uploaded data requires frequent access. However, some data must be retained in cold storage for compliance or archival purposes. Based on your business requirements, you can choose from the following options:

  1. Lifecycle rules based on last modified time: You can use these rules to delete data that has not been modified for an extended period and is no longer needed, or transition it to a cold storage class to free up storage space.

  2. Lifecycle rules based on last access time: You can enable these rules to have OSS automatically monitor data access patterns. OSS identifies infrequently accessed data and transitions it to a more cost-effective cold storage class. This helps you tier data and reduce storage costs.

Notes

  • Before you configure lifecycle rules based on the last modified time or last access time, make sure you understand this feature. For more information, see Lifecycle rules based on the last modified time and Lifecycle rules based on the last access time.

  • The sample code in this topic uses the China (Hangzhou) region (cn-hangzhou) as an example. By default, a public endpoint is used. If you access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.

  • The sample code shows how to obtain access credentials from environment variables. For more information about how to configure access credentials, see Configure access credentials.

  • Setting a lifecycle rule requires the oss:PutBucketLifecycle permission. Viewing lifecycle rules requires the oss:GetBucketLifecycle permission. Deleting all lifecycle rules for a bucket requires the oss:DeleteBucketLifecycle permission. For more information about how to grant permissions, see Grant custom permission policies to RAM users.

Set lifecycle rules

The following code shows how to set lifecycle rules based on the last modified time and the last access time. After you set the rules, if you want to modify one or more of them, see How do I modify one or more lifecycle rule configurations?.

Transition objects based on last modified time

The following code shows how to set a lifecycle rule that transitions objects to a different storage class based on their last modified time.

package main

import (
	"context"
	"flag"
	"log"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)

// Define global variables.
var (
	region     string // The region where the bucket is located.
	bucketName string // The name of the bucket.
)

// The init function initializes command-line arguments.
func init() {
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}

func main() {
	// Parse command-line arguments.
	flag.Parse()

	// Check if the bucket name is provided.
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	// Check if the region is provided.
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// Load the default configuration, and set the credentials provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	// Create an OSS client.
	client := oss.NewClient(cfg)

	// Create a request to set lifecycle rules for the bucket.
	request := &oss.PutBucketLifecycleRequest{
		Bucket: oss.Ptr(bucketName), // The name of the bucket.
		LifecycleConfiguration: &oss.LifecycleConfiguration{
			Rules: []oss.LifecycleRule{
				{
					// Specify lifecycle rule "rule1".
					// This rule transitions objects with the prefix "foo/" and the tag {k1: v1}
					// to the Infrequent Access (IA) storage class 30 days after they are last modified.
					Status: oss.Ptr("Enabled"),
					ID:     oss.Ptr("rule1"),
					Prefix: oss.Ptr("foo/"),
					Transitions: []oss.LifecycleRuleTransition{
						{
							Days:         oss.Ptr(int32(30)),
							StorageClass: oss.StorageClassIA,
							IsAccessTime: oss.Ptr(false), // Set to false for a rule based on last modified time.
						},
					},
					Tags: []oss.Tag{
						{
							Key:   oss.Ptr("k1"),
							Value: oss.Ptr("v1"),
						},
					},
				},
				{
					// Specify lifecycle rule "rule2". This rule applies to objects with the prefix "dir/".
					// It automatically removes expired object delete markers, deletes non-current versions after 30 days,
					// and transitions non-current versions to the IA storage class 10 days after they become non-current.
					ID:     oss.Ptr("rule2"),
					Prefix: oss.Ptr("dir/"),
					Status: oss.Ptr("Enabled"),
					Expiration: &oss.LifecycleRuleExpiration{
						Days:                      oss.Ptr(int32(10)),
						ExpiredObjectDeleteMarker: oss.Ptr(true),
					},
					NoncurrentVersionExpiration: &oss.NoncurrentVersionExpiration{
						NoncurrentDays: oss.Ptr(int32(30)),
					},
					NoncurrentVersionTransitions: []oss.NoncurrentVersionTransition{{
						NoncurrentDays: oss.Ptr(int32(10)),
						StorageClass:   oss.StorageClassIA,
						IsAccessTime:   oss.Ptr(false), // Set to false for a rule based on last modified time.
					}},
				},
			},
		},
	}

	// Set the lifecycle rules for the bucket.
	result, err := client.PutBucketLifecycle(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to put bucket lifecycle %v", err)
	}

	// Print the result of setting the lifecycle rules.
	log.Printf("put bucket lifecycle result:%#v\n", result)
}

Filter objects based on last modified time

The following code shows how to create a lifecycle rule based on the last modified time. This rule applies to objects in a bucket with the prefix logs/ and sizes between 500 and 1,000 bytes. It transitions these objects to the Infrequent Access (IA) storage class 30 days after their last modified time but excludes objects that also have the prefix logs/log and the tag {key1: value1}.

package main

import (
	"context"
	"flag"
	"log"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)

// Define global variables.
var (
	region     string // The region where the bucket is located.
	bucketName string // The name of the bucket.
)

// The init function initializes command-line arguments.
func init() {
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}

func main() {
	// Parse command-line arguments.
	flag.Parse()

	// Check if the bucket name is provided.
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	// Check if the region is provided.
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// Load the default configuration, and set the credentials provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	// Create an OSS client.
	client := oss.NewClient(cfg)

	// Create a request to set lifecycle rules for the bucket.
	request := &oss.PutBucketLifecycleRequest{
		Bucket: oss.Ptr(bucketName),
		LifecycleConfiguration: &oss.LifecycleConfiguration{
			Rules: []oss.LifecycleRule{
				{
					// Specify lifecycle rule "rule1". This rule transitions objects to the IA storage class
					// 30 days after they are last modified. The rule applies to objects with the prefix "logs/",
					// sizes between 500 and 1000 bytes, but excludes those with the prefix "logs/log" and tag {key1: value1}.
					ID:     oss.Ptr("rule1"),
					Status: oss.Ptr("Enabled"),
					Prefix: oss.Ptr("logs/"),
					Transitions: []oss.LifecycleRuleTransition{
						{
							Days:         oss.Ptr(int32(30)),
							StorageClass: oss.StorageClassIA,
							IsAccessTime: oss.Ptr(false), // Set to false for a rule based on last modified time.
						},
					},
					Filter: &oss.LifecycleRuleFilter{
						ObjectSizeGreaterThan: oss.Ptr(int64(500)),
						ObjectSizeLessThan:    oss.Ptr(int64(1000)),
						Nots: []oss.LifecycleRuleNot{
							{
								Prefix: oss.Ptr("logs/log"),
								Tag: &oss.Tag{
									Key:   oss.Ptr("key1"),
									Value: oss.Ptr("value1"),
								},
							},
						},
					},
				},
			},
		},
	}

	// Set the lifecycle rules for the bucket.
	result, err := client.PutBucketLifecycle(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to put bucket lifecycle %v", err)
	}

	// Print the result of setting the lifecycle rules.
	log.Printf("put bucket lifecycle result:%#v\n", result)
}

Transition objects based on last access time

The following code shows how to set a lifecycle rule that transitions objects to a different storage class based on their last access time.

package main

import (
	"context"
	"flag"
	"log"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)

// Define global variables.
var (
	region     string // The region where the bucket is located.
	bucketName string // The name of the bucket.
)

// The init function initializes command-line arguments.
func init() {
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}

func main() {
	// Parse command-line arguments.
	flag.Parse()

	// Check if the bucket name is provided.
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	// Check if the region is provided.
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// Load the default configuration, and set the credentials provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	// Create an OSS client.
	client := oss.NewClient(cfg)

	// Create a request to set lifecycle rules for the bucket.
	request := &oss.PutBucketLifecycleRequest{
		Bucket: oss.Ptr(bucketName), // The name of the bucket.
		LifecycleConfiguration: &oss.LifecycleConfiguration{
			Rules: []oss.LifecycleRule{
				{
					// In lifecycle rule "rule1", transition all objects with the prefix "data/"
					// to the Infrequent Access (IA) storage class 200 days after they are last accessed.
					// When these objects are accessed again, they remain in the IA storage class.
					ID:     oss.Ptr("rule1"),
					Status: oss.Ptr("Enabled"),
					Prefix: oss.Ptr("data/"),
					Transitions: []oss.LifecycleRuleTransition{
						{
							Days:                 oss.Ptr(int32(200)),
							StorageClass:         oss.StorageClassIA,
							IsAccessTime:         oss.Ptr(true), // Set to true for a rule based on last access time.
							ReturnToStdWhenVisit: oss.Ptr(false),
						},
					},
				},
				{
					// In lifecycle rule "rule2", transition all objects with the prefix "log/"
					// to the Infrequent Access (IA) storage class 120 days after they are last accessed.
					// In the same rule, transition these objects to the Archive storage class 250 days after they are last accessed.
					// When these objects are accessed again, they remain in their current storage class.
					ID:     oss.Ptr("rule2"),
					Status: oss.Ptr("Enabled"),
					Prefix: oss.Ptr("log/"),
					Transitions: []oss.LifecycleRuleTransition{
						{
							Days:                 oss.Ptr(int32(120)),
							StorageClass:         oss.StorageClassIA,
							IsAccessTime:         oss.Ptr(true), // Set to true for a rule based on last access time.
							ReturnToStdWhenVisit: oss.Ptr(false),
						},
						{
							Days:                 oss.Ptr(int32(250)),
							StorageClass:         oss.StorageClassArchive,
							IsAccessTime:         oss.Ptr(true),
							ReturnToStdWhenVisit: oss.Ptr(false),
						},
					},
				},
			},
		},
	}

	// Set the lifecycle rules for the bucket.
	result, err := client.PutBucketLifecycle(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to put bucket lifecycle %v", err)
	}

	// Print the result of setting the lifecycle rules.
	log.Printf("put bucket lifecycle result:%#v\n", result)
}

View lifecycle rules

The following code shows how to view a bucket's lifecycle rules.

package main

import (
	"context"
	"flag"
	"fmt"
	"log"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)

// Define global variables.
var (
	region     string // The region where the bucket is located.
	bucketName string // The name of the bucket.
)

// The init function initializes command-line arguments.
func init() {
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}

func main() {
	// Parse command-line arguments.
	flag.Parse()

	// Check if the bucket name is provided.
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	// Check if the region is provided.
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// Load the default configuration, and set the credentials provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	// Create an OSS client.
	client := oss.NewClient(cfg)

	// Create a request to get the lifecycle configuration for the bucket.
	request := &oss.GetBucketLifecycleRequest{
		Bucket: oss.Ptr(bucketName), // The name of the bucket.
	}

	// Get the lifecycle configuration and handle the result.
	result, err := client.GetBucketLifecycle(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to get bucket lifecycle %v", err)
	}

	// Print the information contained in the lifecycle rules.
	for _, rule := range result.LifecycleConfiguration.Rules {
		fmt.Println("Lifecycle Rule Id:", rule.ID)
		fmt.Println("Lifecycle Rule Prefix:", rule.Prefix)
		fmt.Println("Lifecycle Rule Status:", rule.Status)
		if rule.Expiration != nil {
			fmt.Println("Lifecycle Rule Expiration Days:", rule.Expiration.Days)
			fmt.Println("Lifecycle Rule Expiration Created Before Date:", rule.Expiration.CreatedBeforeDate)
			if rule.Expiration.ExpiredObjectDeleteMarker != nil {
				fmt.Println("Lifecycle Rule Expiration Expired Object DeleteMarker:", *rule.Expiration.ExpiredObjectDeleteMarker)
			}
		}

		for _, tag := range rule.Tags {
			fmt.Println("Lifecycle Rule Tag Key:", tag.Key)
			fmt.Println("Lifecycle Rule Tag Value:", tag.Value)
		}

		for _, transition := range rule.Transitions {
			fmt.Println("Lifecycle Rule Transition Days:", transition.Days)
			fmt.Println("Lifecycle Rule Transition Created Before Date:", transition.CreatedBeforeDate)
			fmt.Println("Lifecycle Rule Transition Storage Class:", transition.StorageClass)
			if transition.IsAccessTime != nil {
				fmt.Println("Lifecycle Rule Transition Is Access Time:", *transition.IsAccessTime)
			}
			if transition.ReturnToStdWhenVisit != nil {
				fmt.Println("Lifecycle Rule Transition Return To Std When Visit:", *transition.ReturnToStdWhenVisit)
			}

			if transition.AllowSmallFile != nil {
				fmt.Println("Lifecycle Rule Transition Allow Small File:", *transition.AllowSmallFile)
			}

		}
		if rule.AbortMultipartUpload != nil {
			fmt.Println("Lifecycle Rule Abort Multipart Upload Days:", rule.AbortMultipartUpload.Days)
			fmt.Println("Lifecycle Rule Abort Multipart Upload Created Before Date:", rule.AbortMultipartUpload.CreatedBeforeDate)
		}

		if rule.NoncurrentVersionTransitions != nil {
			for _, nonVersionTransition := range rule.NoncurrentVersionTransitions {
				fmt.Println("Lifecycle Rule Non Version Transitions Non current Days:", nonVersionTransition.NoncurrentDays)
				fmt.Println("Lifecycle Rule Non Version Transition Storage Class:", nonVersionTransition.StorageClass)
				if nonVersionTransition.IsAccessTime != nil {
					fmt.Println("Lifecycle Rule Non Version Transition Is Access Time:", *nonVersionTransition.IsAccessTime)
				}

				if nonVersionTransition.ReturnToStdWhenVisit != nil {
					fmt.Println("Lifecycle Rule Non Version Transition Return To Std When Visit:", *nonVersionTransition.ReturnToStdWhenVisit)
				}

				if nonVersionTransition.AllowSmallFile != nil {
					fmt.Println("Lifecycle Rule Non Version Allow Small File:", *nonVersionTransition.AllowSmallFile)
				}

				if rule.Filter != nil {
					if rule.Filter.ObjectSizeGreaterThan != nil {
						fmt.Println("Lifecycle Rule Filter Object Size Greater Than:", *rule.Filter.ObjectSizeGreaterThan)
					}
					if rule.Filter.ObjectSizeLessThan != nil {
						fmt.Println("Lifecycle Rule Filter Object Size Less Than:", *rule.Filter.ObjectSizeLessThan)
					}
					if len(rule.Filter.Nots) > 0 {
						for _, not := range rule.Filter.Nots {
							fmt.Println("Lifecycle Rule Filter Not Prefix:", not.Prefix)
							if not.Tag != nil {
								fmt.Println("Lifecycle Rule Filter Not Tag Key:", not.Tag.Key)
								fmt.Println("Lifecycle Rule Filter Not Tag Value:", not.Tag.Value)
							}
						}
					}
				}
			}
		}

	}
}

Delete all lifecycle rules

The following code shows how to delete all lifecycle rules for a bucket. To delete one or more specific lifecycle rules, see How do I delete one or more lifecycle rules?.

package main

import (
	"context"
	"flag"
	"log"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)

// Define global variables.
var (
	region     string // The region where the bucket is located.
	bucketName string // The name of the bucket.
)

// The init function initializes command-line arguments.
func init() {
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}

func main() {
	// Parse command-line arguments.
	flag.Parse()

	// Check if the bucket name is provided.
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	// Check if the region is provided.
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// Load the default configuration, and set the credentials provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	// Create an OSS client.
	client := oss.NewClient(cfg)

	// Create a request to delete all lifecycle rules for the bucket.
	request := &oss.DeleteBucketLifecycleRequest{
		Bucket: oss.Ptr(bucketName), // The name of the bucket.
	}

	// Delete all lifecycle rules for the bucket.
	result, err := client.DeleteBucketLifecycle(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to delete bucket lifecycle %v", err)
	}

	// Print the result of deleting the lifecycle rules.
	log.Printf("delete bucket lifecycle result:%#v\n", result)
}

Related topics