This topic describes how to use Go SDK V2 to manage the lifecycle feature for buckets.
Background information
In OSS, not all uploaded data requires frequent access. For data compliance or archiving purposes, some data must be moved to cold storage. You can select one of the following rule types as needed:
Lifecycle rules based on the last modified time: If data is not modified for a long period and no longer needs to be retained, you can use this rule to delete the data in batches or convert it to a cold storage class to free up storage space.
Lifecycle rules based on the last access time: You can enable this rule to have OSS automatically monitor data access patterns, identify cold data, and dynamically convert its storage class. OSS identifies data that has not been accessed for a long period and converts it to a more cost-effective cold storage class. This enables automatic storage tiering and reduces storage costs.
Notes
Before you configure lifecycle rules based on the last modified time or last access time for objects, make sure that you are familiar with 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 (region ID:
cn-hangzhou
) as an example. By default, a public endpoint is used. If you want to access OSS from other Alibaba Cloud products in the same region, you can use an internal endpoint. For more information about the mappings between OSS regions and endpoints, see OSS regions and endpoints.This topic provides an example of how to read access credentials from environment variables. For more information about how to configure access credentials, see Configure access credentials.
To set a lifecycle rule, you must have the
oss:PutBucketLifecycle
permission. To query lifecycle rules, you must have theoss:GetBucketLifecycle
permission. To delete all lifecycle rules, you must have theoss:DeleteBucketLifecycle
permission. For more information, see Grant custom policies to RAM users.
Set lifecycle rules
The following code provides examples of how to set lifecycle rules based on the last modified time and last access time. After you configure the rules, if you want to modify one or more of them, see How do I modify one or more lifecycle rule configurations?.
Convert the storage class of files based on a policy of last modified time
The following code provides an example of how to set a lifecycle rule for a bucket to convert the storage class of files based on the 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 is used to initialize command-line parameters. func init() { flag.StringVar(®ion, "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 bucket name is empty. if len(bucketName) == 0 { flag.PrintDefaults() log.Fatalf("invalid parameters, bucket name required") } // Check whether the region is empty. if len(region) == 0 { flag.PrintDefaults() log.Fatalf("invalid parameters, region required") } // Load the default configurations and set 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 set the 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. In this rule, files that have the prefix foo and the tag key k1 and tag value v1 are converted to the Infrequent Access 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 the value to false. The policy is based on the last modified time. }, }, Tags: []oss.Tag{ { Key: oss.Ptr("k1"), Value: oss.Ptr("v1"), }, }, }, { // Specify lifecycle rule rule2. In this rule, for objects that have the prefix dir in a versioning-enabled bucket, if an object has only a delete marker, the delete marker is automatically deleted. Noncurrent versions of objects expire and are deleted after 30 days. Noncurrent versions of objects are converted to the IA storage class after 10 days. 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 the value to false. The policy is based on the 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 for the bucket. log.Printf("put bucket lifecycle result:%#v\n", result) }
Convert the storage class of files based on a policy of last modified time, excluding files with a specified prefix and tag
The following code provides an example of how to convert the storage class of files in a bucket to Infrequent Access 30 days after they are last modified. This rule applies to files that do not have the prefix log, do not have the tag with the key key1 and value value1, and meet the specified size conditions.
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 is used to initialize command-line parameters. func init() { flag.StringVar(®ion, "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 bucket name is empty. if len(bucketName) == 0 { flag.PrintDefaults() log.Fatalf("invalid parameters, bucket name required") } // Check whether the region is empty. if len(region) == 0 { flag.PrintDefaults() log.Fatalf("invalid parameters, region required") } // Load the default configurations and set 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 set the lifecycle rules for the bucket. request := &oss.PutBucketLifecycleRequest{ Bucket: oss.Ptr(bucketName), LifecycleConfiguration: &oss.LifecycleConfiguration{ Rules: []oss.LifecycleRule{ { // Specify lifecycle rule rule1. In this rule, convert the storage class of files to Infrequent Access 30 days after they are last modified, excluding files that have the prefix log, have the tag with the key key1 and value value1, and meet the specified size conditions. 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 the value to false. The policy is 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"), }, }, }, }, }, }, } // 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 for the bucket. log.Printf("put bucket lifecycle result:%#v\n", result) }
Convert the storage class of files based on a policy of last access time
The following code provides an example of how to set a lifecycle rule for a bucket to convert the storage class of files based on the 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 is used to initialize command-line parameters. func init() { flag.StringVar(®ion, "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 bucket name is empty. if len(bucketName) == 0 { flag.PrintDefaults() log.Fatalf("invalid parameters, bucket name required") } // Check whether the region is empty. if len(region) == 0 { flag.PrintDefaults() log.Fatalf("invalid parameters, region required") } // Load the default configurations and set 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 set the 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 1, specify that all files with the prefix data/ are converted to the Infrequent Access storage class 200 days after they are last accessed. When files with the prefix data/ are accessed again, they remain in the Infrequent Access 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 the value to true. The policy is based on the last access time. ReturnToStdWhenVisit: oss.Ptr(false), }, }, }, { // In lifecycle rule 2, specify that all files with the prefix log/ are converted to the Infrequent Access storage class 120 days after they are last accessed. When files with the prefix log/ are accessed again, they remain in the Infrequent Access storage class. // In the same rule, specify that all files with the prefix log/ are converted to the Archive storage class 250 days after they are last accessed. 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 the value to true. The policy is based on the 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 for the bucket. log.Printf("put bucket lifecycle result:%#v\n", result) }
Query lifecycle rules
The following code provides an example of how to query 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 is used to initialize command-line parameters. func init() { flag.StringVar(®ion, "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 bucket name is empty. if len(bucketName) == 0 { flag.PrintDefaults() log.Fatalf("invalid parameters, bucket name required") } // Check whether the region is empty. if len(region) == 0 { flag.PrintDefaults() log.Fatalf("invalid parameters, region required") } // Load the default configurations and set 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 obtain the lifecycle configuration of the bucket. request := &oss.GetBucketLifecycleRequest{ Bucket: oss.Ptr(bucketName), // The name of the bucket. } // Obtain the lifecycle configuration of the bucket and process 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 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 all lifecycle rules
The following code provides an example of how to delete all lifecycle rules from the examplebucket bucket. If you want to delete one or more 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 is used to initialize command-line parameters. func init() { flag.StringVar(®ion, "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 bucket name is empty. if len(bucketName) == 0 { flag.PrintDefaults() log.Fatalf("invalid parameters, bucket name required") } // Check whether the region is empty. if len(region) == 0 { flag.PrintDefaults() log.Fatalf("invalid parameters, region required") } // Load the default configurations and set 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 all lifecycle rules of the bucket. request := &oss.DeleteBucketLifecycleRequest{ Bucket: oss.Ptr(bucketName), // The name of the bucket. } // Delete all lifecycle rules of 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 all lifecycle rules of the bucket. log.Printf("delete bucket lifecycle result:%#v\n", result) }
References
For more information about the API operation to set lifecycle rules, see PutBucketLifecycle.
For more information about the API operation to query lifecycle rules, see GetBucketLifecycle.
For more information about the API operation to delete all lifecycle rules, see DeleteBucketLifecycle.