Lifecycle rules can take effect on objects with specified prefixes or tags. You can also specify prefixes and tags as the condition of a lifecycle rule at the same time.

Note If you set a specified tag as the condition of a lifecycle rule, the rule applies to an object only when the key and value in the tag of the object both match the specified tag. If you specify a prefix and multiple tags as the condition of a lifecycle rule, the rule applies to an object only when the prefix and tags of the object match the prefix and all tags specified in the rule.

Set tags as the matching condition of a lifecycle rule

Run the following code to set tags as the matching condition of a lifecycle rule:
// This example uses the China East 1 (Hangzhou) endpoint. Specify an actual endpoint based on your requirements.
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// It is highly risky to log on with the AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM user account, log on to https://ram.console.aliyun.com.
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
String bucketName = "<yourBucketName>";

// Creates an OSSClient instance.
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

// Creates a SetBucketLifecycleRequest.
SetBucketLifecycleRequest request = new SetBucketLifecycleRequest(bucketName);

// Sets the rule ID and the prefixes and tags used as the matching condition.
String ruleId0 = "rule0";
String matchPrefix0 = "A0/";
Map<String, String> matchTags0 = new HashMap<String, String>();
matchTags0.put("key0", "value0");


String ruleId1 = "rule1";
String matchPrefix1 = "A1/";
Map<String, String> matchTags1 = new HashMap<String, String>();
matchTags1.put("key1", "value1");


String ruleId2 = "rule2";
String matchPrefix2 = "A2/";

String ruleId3 = "rule3";
String matchPrefix3 = "A3/";

// Sets an expiration rule for objects so that objects are expired three days after they are modified for the last time.
LifecycleRule rule = new LifecycleRule(ruleId0, matchPrefix0, RuleStatus.Enabled, 3);
rule.setTags(matchTags0)
request.AddLifecycleRule(rule);

// Sets an expiration rule for objects so that objects created before the specified date are expired.
rule = new LifecycleRule(ruleId1, matchPrefix1, RuleStatus.Enabled);
rule.setCreatedBeforeDate(DateUtil.parseIso8601Date("2022-10-12T00:00:00.000Z"));
rule.setTags(matchTags1)
request.AddLifecycleRule(rule);

// Sets an expiration rules for parts so that parts are expired 3 days after they are generated.
rule = new LifecycleRule(ruleId2, matchPrefix2, RuleStatus.Enabled);
LifecycleRule.AbortMultipartUpload abortMultipartUpload = new LifecycleRule.AbortMultipartUpload();
abortMultipartUpload.setExpirationDays(3);
rule.setAbortMultipartUpload(abortMultipartUpload);
request.AddLifecycleRule(rule);

// Sets an expiration rules for parts so that parts generated before the specified date are expired.
rule = new LifecycleRule(ruleId3, matchPrefix3, RuleStatus.Enabled);
abortMultipartUpload = new LifecycleRule.AbortMultipartUpload();
abortMultipartUpload.setCreatedBeforeDate(DateUtil.parseIso8601Date("2022-10-12T00:00:00.000Z"));
rule.setAbortMultipartUpload(abortMultipartUpload);
request.AddLifecycleRule(rule);

ossClient.setBucketLifecycle(request);

// Closes the OSSClient.
ossClient.shutdown();

View the tags set as the matching condition of a lifecycle rule

Run the following code to view the tags set as the matching condition of a lifecycle rule:
// This example uses the China East 1 (Hangzhou) endpoint. Specify an actual endpoint based on your requirements.
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// It is highly risky to log on with the AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM user account, log on to https://ram.console.aliyun.com.
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
String bucketName = "<yourBucketName>";

// Creates an OSSClient.
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

// Obtains the lifecycle rule set for the object.
List<LifecycleRule> rules = ossClient.getBucketLifecycle(bucketName);

// Views the lifecycle rule set for the object.
for (LifecycleRule rule1 : rules) {
    // Views the rule ID.
    System.out.println("rule id: " + rule1.getId());

    // Views the prefixes set as the matching condition.
    System.out.println("rule prefix: " + rule1.getPrefix());

    // Views the tags set as the matching condition.
    if (rule1.hasTags()) {
     System.out.println("rule tagging: "+ rule1.getTags().toString());
    }

    // Views the expiration rules in which objects are expired some days after they are modified for the last time.
    if (rule1.hasExpirationDays()) {
     System.out.println("rule expiration days: " + rule1.getExpirationDays());
    }

    // Views the expiration rules in which objects created before a specified date are expired.
    if (rule1.hasCreatedBeforeDate()) {
      System.out.println("rule expiration create before days: " + rule1.getCreatedBeforeDate());
    }

    // Views the expiration rule for parts.
    if(rule1.hasAbortMultipartUpload()) {
        if(rule1.getAbortMultipartUpload().hasExpirationDays()) {
          System.out.println("rule abort uppart days: " + rule1.getAbortMultipartUpload().getExpirationDays());
        }

        if (rule1.getAbortMultipartUpload().hasCreatedBeforeDate()) {
          System.out.println("rule abort uppart create before date: " + rule1.getAbortMultipartUpload().getCreatedBeforeDate());
        }
    }
}

// Closes the OSSClient.
ossClient.shutdown();