You can configure lifecycle rules for objects with specified tags and objects whose names contain specified prefixes. You can also specify a combination of prefixes and tags as conditions of lifecycle rules.
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.
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.
-
This topic demonstrates creating an OSSClient instance with an OSS endpoint. For alternative configurations, such as using a custom domain or authenticating with credentials from Security Token Service (STS), see Configure a client (Go SDK V1).
Specify tags in a lifecycle rule
The following sample code provides an example on how to specify tags 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 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 your actual endpoint.
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Specify the version of the signature algorithm.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Configure a lifecycle rule whose ID is rule1. This lifecyle rule applies to objects whose names contain the "one" prefix.
// The lifecycle rule moves objects whose names contain the "one" prefix to the IA storage class 3 days after the last modified time and then to the Archive storage class 30 days after the last modified time.
transitionIA := oss.LifecycleTransition{
Days: 3,
StorageClass: oss.StorageIA,
}
transitionArch := oss.LifecycleTransition{
Days: 30,
StorageClass: oss.StorageArchive,
}
// Configure tags.
tag1 := oss.Tag{
Key: "key1",
Value: "value1",
}
tag2 := oss.Tag{
Key: "key2",
Value: "value2",
}
// Configure a lifecycle rule for the bucket. The lifecycle rule contains the configured tags.
rule1 := oss.LifecycleRule{
ID: "rule1",
Prefix: "one",
Status: "Enabled",
Transitions: []oss.LifecycleTransition{transitionIA, transitionArch},
Tags: []oss.Tag{tag1, tag2},
}
rules := []oss.LifecycleRule{rule1}
err = client.SetBucketLifecycle("yourBucketName", rules)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
}
Query tags configured in a lifecycle rule
The following code provides an example on how to query tags configured 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 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 your actual endpoint.
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Specify the version of the signature algorithm.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Query the lifecycle rule that is configured for the bucket.
// Specify the name of the bucket.
lc, err := client.GetBucketLifecycle("yourBucketName")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Display the lifecycle rule information, which includes tag 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 tag-based lifecycle management, visit GitHub.
-
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.