Not all data that is uploaded to Object Storage Service (OSS) buckets is frequently accessed. Rarely accessed data is stored in cold storage to meet compliance and archiving requirements. If you want to delete data stored in multiple OSS buckets at the same time, you can configure lifecycle rules based on the last modified time of the data. You can configure lifecycle rules based on the last access time of data stored in a bucket to implement tiered storage of hot and cold data and reduce storage costs. After you configure a lifecycle rule, OSS monitors the access patterns of the objects in the bucket, identifies cold data based on access patterns, and automatically changes the storage class of cold data.
Usage 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 rules based on the last modified time and Lifecycle rules based on the last access time.
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see 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.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Initialization.
To configure lifecycle rules, you must have the
oss:PutBucketLifecycle
permission. To query lifecycle rules, you must have theoss:GetBucketLifecycle
permission. To clear lifecycle rules, you must have theoss:DeleteBucketLifecycle
permission. For more information, see Attach a custom policy to a RAM user.
Configure lifecycle rules
The following sample 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 change the configurations of one or more lifecycle rules?
Configure a lifecycle rule based on the last modified time to change the storage classes of objects or delete objects
The following sample code provides an example on how to configure a lifecycle rule based on the last modified time of objects in a bucket named examplebucket to change the storage classes of objects or delete the expired objects:
package main
import (
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"os"
)
func main() {
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the actual endpoint.
client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Specify the name of the bucket.
bucketName := "examplebucket"
// Configure a lifecycle rule and set ID to rule1. Specify that the objects whose names contain the foo prefix in the bucket expire three days after the objects are last modified.
rule1 := oss.BuildLifecycleRuleByDays("rule1", "foo/", true, 3)
// If an object in a bucket for which versioning is enabled is a delete marker and has no other versions, the delete marker is deleted.
deleteMark := true
expiration := oss.LifecycleExpiration{
ExpiredObjectDeleteMarker: &deleteMark,
}
// Specify that the previous versions of objects are deleted 30 days after they are last modified.
versionExpiration := oss.LifecycleVersionExpiration{
NoncurrentDays: 30,
}
// Specify that the storage classes of the previous versions of objects are converted to Infrequent Access (IA) 10 days after the objects are last modified.
versionTransition := oss.LifecycleVersionTransition{
NoncurrentDays: 10,
StorageClass: "IA",
}
// Configure a lifecycle rule and set ID to rule2.
rule2 := oss.LifecycleRule{
ID: "rule2",
Prefix: "yourObjectPrefix",
Status: "Enabled",
Expiration: &expiration,
NonVersionExpiration: &versionExpiration,
NonVersionTransitions: []oss.LifecycleVersionTransition{
versionTransition,
},
}
// Configure a lifecycle rule and set ID to rule3. This rule takes effect for objects that have the tag whose key is tag1 and value is value1. These objects expire three days after the objects are last modified.
rule3 := oss.LifecycleRule{
ID: "rule3",
Prefix: "",
Status: "Enabled",
Tags: []oss.Tag{
oss.Tag{
Key: "tag1",
Value: "value1",
},
},
Expiration: &oss.LifecycleExpiration{Days: 3},
}
// Configure lifecycle rules.
rules := []oss.LifecycleRule{rule1, rule2, rule3}
err = client.SetBucketLifecycle(bucketName, rules)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
}
Configure a lifecycle rule based on the last modified time to convert the storage class of objects, except for the objects whose names contain specific prefixes or that have specific tags
The following sample code provides an example on how to configure a lifecycle rule to convert the storage class of the following objects in the examplebucket bucket to IA 30 days after the objects are last modified: objects whose names do not contain the log prefix, objects that do not have a tag whose key is key1 and value is value1, and objects that do not meet the specified size requirements. The filtering conditions are specified in the Not element of the filter node.
package main
import (
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"os"
)
func main() {
// Specify the name of the bucket.
bucketName := "yourBucketName"
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the actual endpoint.
client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
tag := oss.Tag{
Key: "key1",
Value: "value1",
}
// Specify the minimum size of an object on which the lifecycle rule takes effect.
size := int64(500)
// Specify the maximum size of an object on which the lifecycle rule takes effect.
smallSize := int64(64500)
filter := oss.LifecycleFilter{
ObjectSizeGreaterThan: &size,
ObjectSizeLessThan: &smallSize,
Not: []oss.LifecycleFilterNot{
{
Prefix: "logs/log",
Tag: &tag,
},
},
}
rule1 := oss.LifecycleRule{
ID: "mtime transition1",
Prefix: "logs",
Status: "Enabled",
Transitions: []oss.LifecycleTransition{
{
Days: 30,
StorageClass: oss.StorageIA,
},
},
Filter: &filter,
}
var rules = []oss.LifecycleRule{rule1}
err = client.SetBucketLifecycle(bucketName, rules)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
fmt.Printf("%s\n", "set lifecycle successed")
}
Configure a lifecycle rule based on the last access time to convert the storage class of objects
The following sample code provides an example on how to configure a lifecycle rule based on the last access time to convert the storage classes of the objects in the examplebucket bucket:
package main
import (
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"os"
)
func main() {
// Specify the name of the bucket.
bucketName := "yourBucketName"
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the actual endpoint.
client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
isTrue := true
isFalse := false
// Configure a lifecycle rule and set 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. In addition, specify that the objects whose name contain the logs prefix are still stored as IA objects when the objects are accessed again.
rule1 := oss.LifecycleRule{
ID: "rule1",
Prefix: "logs",
Status: "Enabled",
Transitions: []oss.LifecycleTransition{
{
Days: 30,
StorageClass: oss.StorageIA,
IsAccessTime: &isTrue,
ReturnToStdWhenVisit: &isFalse,
AllowSmallFile: &isTrue,
},
},
}
// Configure a lifecycle rule and set 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 whose names contain the dir prefix are converted to Standard when the objects are accessed again.
rule2 := oss.LifecycleRule{
ID: "rule2",
Prefix: "dir",
Status: "Enabled",
NonVersionTransitions: []oss.LifecycleVersionTransition{
{
NoncurrentDays: 10,
StorageClass: oss.StorageIA,
IsAccessTime: &isTrue,
ReturnToStdWhenVisit: &isTrue,
AllowSmallFile: &isFalse,
},
},
}
// Configure lifecycle rules.
var rules = []oss.LifecycleRule{rule1, rule2}
err = client.SetBucketLifecycle(bucketName, rules)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
fmt.Printf("%s\n", "set bucket life cycle success")
}
Query the lifecycle rules of a bucket
The following code provides an example on how to query the lifecycle rules configured for the bucket named examplebucket:
package main
import (
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"os"
)
func main() {
// Specify the name of the bucket.
bucketName := "yourBucketName"
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the actual endpoint.
client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
lcRes, err := client.GetBucketLifecycle(bucketName)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
for _, rule := range lcRes.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 Date:", rule.Expiration.Date)
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.NonVersionExpiration != nil {
fmt.Println("Lifecycle Non Version Expiration Non Current Days:", rule.NonVersionExpiration.NoncurrentDays)
}
for _, nonVersionTransition := range rule.NonVersionTransitions {
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)
}
for _, filterNot := range rule.Filter.Not {
fmt.Println("Lifecycle Rule Filter Not Prefix:", filterNot.Prefix)
if filterNot.Tag != nil {
fmt.Println("Lifecycle Rule Filter Not Tag Key:", filterNot.Tag.Key)
fmt.Println("Lifecycle Rule Filter Not Tag Value:", filterNot.Tag.Value)
}
}
}
}
}
}
Delete the lifecycle rules of a bucket
The following code provides an example on how to clear lifecycle rules in a bucket named examplebucket. If you want to delete one or more lifecycle rules, refer to How do I delete one or more lifecycle rules that are configured for a bucket?.
package main
import (
"fmt"
"os"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Specify the name of the bucket.
bucketName := "yourBucketName"
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the actual endpoint.
client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Delete the lifecycle rules.
err = client.DeleteBucketLifecycle(bucketName)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
}
References
For the complete sample code that is used to manage lifecycle rules, visit GitHub.
For more information about the API operation that you can call to configure a lifecycle rule, 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.