All Products
Search
Document Center

Object Storage Service:Use object tagging for lifecycle management

Last Updated:Oct 09, 2023

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.

Note 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 lifecycle 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 by using other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about the regions and endpoints supported by OSS, 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.

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. 
	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
	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. 
	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
	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.