对象标签和生命周期管理
生命周期规则可针对前缀或对象标签生效,您也可以同时指定两者作为生命周期规则生效的条件。
说明 Tag条件中,标签的Key和Value必须同时匹配。同一个规则中,若同时配置了前缀和多个对象标签,则对象需满足前缀,且同时匹配规则中所有对象标签,才视为适用于该规则。
生命周期规则中添加标签匹配规则
以下代码用于生命周期规则中添加标签匹配规则:
// Endpoint以杭州为例,其它Region请按实际情况填写。
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
String bucketName = "<yourBucketName>";
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 创建SetBucketLifecycleRequest
SetBucketLifecycleRequest request = new SetBucketLifecycleRequest(bucketName);
// 设置规则ID、文件前缀与标签。
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/";
// 距最后修改时间3天后过期。
LifecycleRule rule = new LifecycleRule(ruleId0, matchPrefix0, RuleStatus.Enabled, 3);
rule.setTags(matchTags0)
request.AddLifecycleRule(rule);
// 指定日期之前创建的文件过期。
rule = new LifecycleRule(ruleId1, matchPrefix1, RuleStatus.Enabled);
rule.setCreatedBeforeDate(DateUtil.parseIso8601Date("2022-10-12T00:00:00.000Z"));
rule.setTags(matchTags1)
request.AddLifecycleRule(rule);
// 分片3天后过期。
rule = new LifecycleRule(ruleId2, matchPrefix2, RuleStatus.Enabled);
LifecycleRule.AbortMultipartUpload abortMultipartUpload = new LifecycleRule.AbortMultipartUpload();
abortMultipartUpload.setExpirationDays(3);
rule.setAbortMultipartUpload(abortMultipartUpload);
request.AddLifecycleRule(rule);
// 指定日期之前的分片过期。
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);
// 关闭OSSClient。
ossClient.shutdown();
查看生命周期规则中匹配的标签信息
以下代码用于查看生命周期规则中匹配的标签信息:
// Endpoint以杭州为例,其它Region请按实际情况填写。
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
String bucketName = "<yourBucketName>";
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 获取生命周期规则。
List<LifecycleRule> rules = ossClient.getBucketLifecycle(bucketName);
// 查看生命周期规则。
for (LifecycleRule rule1 : rules) {
// 查看规则id。
System.out.println("rule id: " + rule1.getId());
// 查看规则前缀。
System.out.println("rule prefix: " + rule1.getPrefix());
// 查看规则标签。
if (rule1.hasTags()) {
System.out.println("rule tagging: "+ rule1.getTags().toString());
}
//查看过期天数规则。
if (rule1.hasExpirationDays()) {
System.out.println("rule expiration days: " + rule1.getExpirationDays());
}
//查看过期日期规则。
if (rule1.hasCreatedBeforeDate()) {
System.out.println("rule expiration create before days: " + rule1.getCreatedBeforeDate());
}
//查看过期分片规则。
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());
}
}
}
// 关闭OSSClient。
ossClient.shutdown();
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。
评论