Lifecycle rules can be applied based on object prefixes or object tags. You can also specify both as conditions for a lifecycle rule.
If you configure tag conditions, the rule applies only to objects that meet the tag key and value conditions. If a prefix and multiple object tags are configured in a rule, the rule applies only to objects that match the prefix and object tag conditions.
Usage notes
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 Configure OSSClient instances.
Add a tag matching rule to a lifecycle rule
The following code shows how to add a tag matching rule to a lifecycle rule:
package main
import (
"fmt"
"os"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Replace yourEndpoint with the Endpoint of the bucket. For example, for the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual Endpoint.
// Replace yourRegion with the region where the bucket is located. For example, for the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Set the signature version.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Set the ID of the lifecycle rule to "rule1" and apply the rule to objects that have the "one" prefix.
// Objects with this prefix are converted to the IA storage class after 3 days and to the Archive storage class after 30 days.
transitionIA := oss.LifecycleTransition{
Days: 3,
StorageClass: oss.StorageIA,
}
transitionArch := oss.LifecycleTransition{
Days: 30,
StorageClass: oss.StorageArchive,
}
// Set the object tagging.
tag1 := oss.Tag{
Key: "key1",
Value: "value1",
}
tag2 := oss.Tag{
Key: "key2",
Value: "value2",
}
// Set the lifecycle rule for the bucket. The lifecycle rule contains the configured object tagging information.
rule1 := oss.LifecycleRule{
ID: "rule1",
Prefix: "one",
Status: "Enabled",
Transitions: []oss.LifecycleTransition{transitionIA, transitionArch},
Tags: []oss.Tag{tag1, tag2},
}
rules := []oss.LifecycleRule{rule1}
// Replace yourBucketName with the name of the bucket.
err = client.SetBucketLifecycle("yourBucketName", rules)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
}
View tag information in a lifecycle rule
The following code shows how to view the tag information in a lifecycle rule:
package main
import (
"fmt"
"os"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Replace yourEndpoint with the Endpoint of the bucket. For example, for the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual Endpoint.
// Replace yourRegion with the region where the bucket is located. For example, for the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Set the signature version.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Obtain the lifecycle rules of the bucket.
// Replace yourBucketName with the name of the bucket.
lc, err := client.GetBucketLifecycle("yourBucketName")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Print the lifecycle information, which includes the object tagging information.
for i, v := range lc.Rules {
fmt.Printf("Bucket %d Lifecycle: %v", i, v)
if v.Expiration != nil {
fmt.Printf(", Expiration:%v", *v.Expiration)
}
fmt.Printf("\n")
}
}
References
For the complete sample code for object tagging and lifecycle management, see GitHub example.
For more information about the API operation used to set lifecycle rules, see PutBucketLifecycle.
For more information about the API operation used to view lifecycle rules, see GetBucketLifecycle.