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 rule, the rule applies only to objects that match the prefix and object tag conditions.

Use tags in a lifecycle rule

The following code provides an example on how to use tags in a lifecycle rule:
package main

import (
  "fmt"
  "os"

  "github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
  // 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. 
  // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
  client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
  if err != nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }

  // Configure a lifecycle rule whose ID is rule1 and that 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 three 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 whose ID is rule 1 for the bucket. The lifecycle rule applies to objects whose names contain the "one" prefix. 
  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() {
  // 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. 
  // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
  client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
  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.