设置桶生命周期

生命周期管理可以通过设置规则实现自动清理过期的对象,优化存储空间。本文介绍如何设置桶(Bucket)生命周期规则。您可以通过setBucketLifecycleConfiguration操作可以设置桶的生命周期规则,规则可以通过匹配对象key前缀、标签的方法设置当前版本或者历史版本对象的过期时间,对象过期后会被自动删除。

桶的版本控制状态必须处于Enabled或者Suspended,历史版本对象过期时间配置才能生效。每次执行setBucketLifecycleConfiguration操作会覆盖桶中已存在的生命周期规则。以下为示例代码

public void setBucketLifecycleConfiguration() throws AmazonClientException {
	System.out.println("setBucketLifecycleConfiguration");
	BucketLifecycleConfiguration config = new BucketLifecycleConfiguration();
	List<BucketLifecycleConfiguration.Rule> rules = new ArrayList<BucketLifecycleConfiguration.Rule>();
	// mtime rule
	BucketLifecycleConfiguration.Transition transition = new BucketLifecycleConfiguration.Transition();
	transition.setDays(1);
	transition.setStorageClass(StorageClass.StandardInfrequentAccess);
	BucketLifecycleConfiguration.NoncurrentVersionTransition ncvtransition =
			new BucketLifecycleConfiguration.NoncurrentVersionTransition();
	ncvtransition.setDays(2);
	ncvtransition.setStorageClass(StorageClass.StandardInfrequentAccess);
	// atime rule
	BucketLifecycleConfiguration.AtimeTransition aTimeTransition = new BucketLifecycleConfiguration.AtimeTransition();
	aTimeTransition.setDays(3);
	aTimeTransition.setStorageClass(StorageClass.StandardInfrequentAccess);
	aTimeTransition.setTransToStandard(true);
	aTimeTransition.setNoTransMaxSize(1024*1024);
	BucketLifecycleConfiguration.AtimeNoncurrentVersionTransition aTimeNcvtransition =
			new BucketLifecycleConfiguration.AtimeNoncurrentVersionTransition();
	aTimeNcvtransition.setDays(4);
	aTimeNcvtransition.setStorageClass(StorageClass.StandardInfrequentAccess);
	aTimeNcvtransition.setTransToStandard(true);
	aTimeNcvtransition.setNoTransMaxSize(1024*1024);

	// incomplete upload rule
	AbortIncompleteMultipartUpload abort = new AbortIncompleteMultipartUpload();
	abort.setDaysAfterInitiation(20);

	BucketLifecycleConfiguration.Rule rule = new BucketLifecycleConfiguration.Rule();
	rule.setId("haha");
	rule.setExpirationInDays(10);
	rule.setAbortIncompleteMultipartUpload(abort);
	rule.setStatus(BucketLifecycleConfiguration.ENABLED);
	// 设置应用范围,可以指定前缀或对象标签
	//LifecycleFilterPredicate predicate = new LifecyclePrefixPredicate("abc/");
	//rule.setFilter(new LifecycleFilter(predicate));
	//LifecycleFilterPredicate predicate = new LifecycleTagPredicate(new Tag("your-key", "your-value"));
	//rule.setFilter(new LifecycleFilter(predicate));
	rule.addTransition(transition);
	rule.addNoncurrentVersionTransition(ncvtransition);
	rule.addAtimeTransition(aTimeTransition);
	rule.addAtimeNoncurrentVersionTransition(aTimeNcvtransition);
	rules.add(rule);
	config.setRules(rules);
	SetBucketLifecycleConfigurationRequest req = new SetBucketLifecycleConfigurationRequest(bucket, config);
	s3Client.setBucketLifecycleConfiguration(req);
	System.out.println("setBucketLifecycleConfiguration success");
}

规则参数如下:

参数 类型 说明
id String 规则ID
status String(Enabled|Disabled) 是否启用规则
expirationInDays int 文件过期时间
noncurrentVersionExpirationInDays int 历史版本过期时间
abortIncompleteMultipartUpload AbortIncompleteMultipartUpload 未完成上传的分片过期时间
transition Transition 文件转换到低频存储规则(距离修改时间)
noncurrentVersionTransition NoncurrentVersionTransition 历史版本转换到低频存储规则(距离修改时间)
atimeTransition AtimeTransition 文件转换到低频存储规则(距离访问时间)
atimeNoncurrentVersionTransition AtimeNoncurrentVersionTransition 历史版本转换到低频存储规则(距离访问时间)
filter LifecycleFilterPredicate 应用范围,可以指定前缀或对象标签

Transition:

参数 类型 说明
days int 转换过期天数,默认-1,表示不转换
storageClass StorageClass 要转换的存储类型

AtimeTransition:

参数 类型 说明
days int 转换过期天数,默认-1,表示不转换
storageClass StorageClass 要转换的存储类型
transToStandard boolean 是否在下次访问时转换回标准存储,默认false表示不转回标准存储
noTransMaxSize long 低于此大小的文件不进行转换,默认-1表示所有文件都转换

响应结果:

HTTP状态 响应码 描述
200 Success 操作成功。
400 MalformedXML 生命周期规则不合法。
403 AccessDenied 用户没有权限执行操作。

注意:atime规则只在官网的sdk提供,AWS S3没有atime规则。只有部分资源池支持atime规则。