All Products
Search
Document Center

Object Storage Service:Lifecycle management

Last Updated:Jan 06, 2025

This topic describes how to manage lifecycle rules using OSS SDK for Go V2.

Background information

Based on your business requirements, you may need to retain some infrequently accessed data in cold storage due to compliance or archiving requirements.

  1. Lifecycle rules based on the last modified time: You can configure lifecycle rules based on the last modified time to delete data that has not been modified for an extended period or is no longer needed in batches, or to convert the storage class to free up space.

  2. Lifecycle rules based on the last access time: After you configure lifecycle rules based on the last access time, OSS will monitor the access patterns of the objects in the bucket, identify infrequently accessed data, and dynamically convert its storage class for tiered data storage and reduced storage costs.

Notes

  • Before you configure lifecycle rules based on the last modified time or last access time of objects, make sure that you familiarize yourself with this feature. For more information, see lifecycle rule based on the last modified time and lifecycle rule based on the last access time.

  • The sample code in this topic uses the region ID cn-hangzhou of the China (Hangzhou) region. By default, the public endpoint is used to access resources in a bucket. If you want to access resources in the bucket by using other Alibaba Cloud services in the same region in which the bucket is located, use an internal endpoint. For more information about the regions and endpoints supported by Object Storage Service (OSS), see OSS regions and endpoints.

  • In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.

  • The oss:PutBucketLifecycle permission is required to configure a lifecycle rule. The oss:GetBucketLifecycle permission is required to query a lifecycle rule. The oss:DeleteBucketLifecycle permission is required to delete a lifecycle rule. For more information, see Grant custom permission policies to RAM users.

Configure lifecycle rules

The following code provides examples of lifecycle rules that are configured based on the last modified time and last access time of data. To modify lifecycle rules, follow the instructions described in How do I modify a configuration item in one or more lifecycle rules for a bucket?.

Configure a lifecycle rule based on the last modified time to convert the storage class of objects 

The following code provides an example of how to configure a lifecycle rule based on the last modified time for a bucket to convert the storage class of objects within.

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 // Region in which the bucket is located.
	bucketName string // Name of the bucket.
)

// Specify the init function used to initialize command line parameters.
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 parameters.
	flag.Parse()

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

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

	// Load the default configurations and specify the credential provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

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

	// Create a request to configure lifecycle rules for the bucket.
	request := &oss.PutBucketLifecycleRequest{
		Bucket: oss.Ptr(bucketName), // Name of the bucket.
		LifecycleConfiguration: &oss.LifecycleConfiguration{
			Rules: []oss.LifecycleRule{
				{
					// Set the lifecycle rule ID to rule1. Specify that the storage class of objects tagged with the key 'k1' and value 'v1' whose names contain the foo prefix in the bucket are converted to IA 30 days after the objects 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 this parameter to false to specify that the storage classes of objects are converted based on the last modified time.
						},
					},
					Tags: []oss.Tag{
						{
							Key:   oss.Ptr("k1"),
							Value: oss.Ptr("v1"),
						},
					},
				},
				{
					// Set the lifecycle rule ID to rule2. If objects whose names contain the dir prefix in a versioning-enabled bucket are delete markers and have no other versions, the delete markers are removed. The previous versions of objects are deleted 30 days after they are last modified. The storage classes of the previous versions of objects are converted to Infrequent Access (IA) 10 days after the objects are last modified.
					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 this parameter to false.
					}},
				},
			},
		},
	}

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

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

Configure a lifecycle rule based on the last modified time to convert the storage class of objects, excluding the objects whose names contain specific prefixes or that have specific tags

The following code provides an example on how to configure a lifecycle rule based on the last modified time. The lifecycle rule is used to convert the storage class of objects that meet the following conditions in the bucket to IA 30 days after the objects are last modified: The names of the objects do not contain the log prefix and the objects do not have the tag whose key is tag1 and whose value is 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 // Region in which the bucket is located.
	bucketName string // Name of the bucket.
)

// Specify the init function used to initialize command line parameters.
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 parameters.
	flag.Parse()

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

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

	// Load the default configurations and specify the credential provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

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

	// Create a request to configure lifecycle rules for the bucket.
	request := &oss.PutBucketLifecycleRequest{
		Bucket: oss.Ptr(bucketName),
		LifecycleConfiguration: &oss.LifecycleConfiguration{
			Rules: []oss.LifecycleRule{
				{
					// Set the lifecycle rule ID to rule1. The rule is used to convert the storage class of objects that meet the following conditions in the bucket to IA 30 days after the objects are last modified: The names of the objects do not contain the log prefix and the objects do not have the tag whose key is tag1 and whose value is 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 this parameter to false to specify that the storage classes of objects are converted based on the last modified time.
						},
					},
					Filter: &oss.LifecycleRuleFilter{
						ObjectSizeGreaterThan: oss.Ptr(int64(500)),
						ObjectSizeLessThan:    oss.Ptr(int64(1000)),
						Not: &oss.LifecycleRuleNot{
							Prefix: oss.Ptr("logs/log"),
							Tag: &oss.Tag{
								Key:   oss.Ptr("key1"),
								Value: oss.Ptr("value1"),
							},
						},
					},
				},
			},
		},
	}

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

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

Configure a lifecycle rule based on the last access time to convert the storage class of objects

The following code provides an example of how to configure a lifecycle rule based on the last access time to convert the storage classes of objects in the bucket.

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 // Region in which the bucket is located.
	bucketName string // Name of the bucket.
)

// Specify the init function used to initialize command line parameters.
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 parameters.
	flag.Parse()

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

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

	// Load the default configurations and specify the credential provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

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

	// Create a request to configure lifecycle rules for the bucket.
	request := &oss.PutBucketLifecycleRequest{
		Bucket: oss.Ptr(bucketName), // Name of the bucket.
		LifecycleConfiguration: &oss.LifecycleConfiguration{
			Rules: []oss.LifecycleRule{
				{
					// Set the lifecycle rule ID to rule1. Specify that the storage classes of objects whose names contain the logs prefix and whose size is less than or equal to 64 KB are converted to IA 30 days after the objects are last accessed, and remain in IA when they are accessed again. 
					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(true), // Set this parameter to true to specify that the storage classes of objects are converted based on the last access time.
							ReturnToStdWhenVisit: oss.Ptr(false),
							AllowSmallFile:       oss.Ptr(true),
						},
					},
				},
				{
					// Set the lifecycle rule ID to rule2. Specify that the previous versions of the objects whose names contain the dir prefix and whose size is greater than 64 KB are converted to IA 10 days after the objects are last accessed. In addition, specify that the storage classes of the objects are changed to Standard when the objects are accessed again.
					ID:     oss.Ptr("rule2"),
					Status: oss.Ptr("Enabled"),
					Prefix: oss.Ptr("dir/"),
					NoncurrentVersionTransitions: []oss.NoncurrentVersionTransition{
						{
							NoncurrentDays:       oss.Ptr(int32(10)),
							StorageClass:         oss.StorageClassIA,
							IsAccessTime:         oss.Ptr(true), // Set this parameter to true to specify that the storage classes of objects are converted based on the last access time.
							ReturnToStdWhenVisit: oss.Ptr(true),
							AllowSmallFile:       oss.Ptr(false),
						},
					},
				},
			},
		},
	}

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

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

Query the lifecycle rules of a bucket

The following code provides an example of how to query the lifecycle rules configured for a bucket.

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 // Region in which the bucket is located.
	bucketName string // Name of the bucket.
)

// Specify the init function used to initialize command line parameters.
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 parameters.
	flag.Parse()

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

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

	// Load the default configurations and specify the credential provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

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

	// Create a request to query the lifecycle configurations of the bucket.
	request := &oss.GetBucketLifecycleRequest{
		Bucket: oss.Ptr(bucketName), // Name of the bucket.
	}

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

	// Display the lifecycle configurations.
	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 rule.Filter.Not != nil {
						fmt.Println("Lifecycle Rule Filter Not Prefix:", rule.Filter.Not.Prefix)
						if rule.Filter.Not.Tag != nil {
							fmt.Println("Lifecycle Rule Filter Not Tag Key:", rule.Filter.Not.Tag.Key)
							fmt.Println("Lifecycle Rule Filter Not Tag Value:", rule.Filter.Not.Tag.Value)
						}
					}
				}
			}
		}

	}
}

Delete the lifecycle rules of a bucket

The following code provides an example of how to delete the lifecycle rules configured for a bucket. To delete one or more lifecycle rules, follow the instructions as described in How to 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 // Region in which the bucket is located.
	bucketName string // Name of the bucket.
)

// Specify the init function used to initialize command line parameters.
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 parameters.
	flag.Parse()

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

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

	// Load the default configurations and specify the credential provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

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

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

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

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

References

  • For more information about the API operation that you can call to configure lifecycle rules, see PutBucketLifecycle.

  • For more information about the API operation that you can call to query lifecycle rules, see GetBucketLifecycle.

  • For more information about the API operation that you can call to delete lifecycle rules, see DeleteBucketLifecycle.