查看桶生命周期

生命周期管理可以通过设置规则实现自动清理过期的对象,优化存储空间。本文介绍如何查看桶(Bucket)的生命周期规则。以下为示例代码

public void getBucketLifecycleConfiguration() throws AmazonClientException {
	System.out.println("getBucketLifecycleConfiguration");
	BucketLifecycleConfiguration config = s3Client.getBucketLifecycleConfiguration(bucket);
	List<BucketLifecycleConfiguration.Rule> rules = config.getRules();
	for (BucketLifecycleConfiguration.Rule rule: rules){
		System.out.println("getBucketLifecycleConfiguration: " + rule.getId());
		System.out.println("getBucketLifecycleConfiguration expire: " +
				rule.getExpirationInDays());
		if (rule.getAbortIncompleteMultipartUpload() != null){
			System.out.println("getBucketLifecycleConfiguration abort: " +
					rule.getAbortIncompleteMultipartUpload().getDaysAfterInitiation());
		}

		for (BucketLifecycleConfiguration.Transition tran: rule.getTransitions()){
			System.out.println("getBucketLifecycleConfiguration tran: " + tran.getStorageClassAsString() + "," +
					tran.getDays());
		}
		for (BucketLifecycleConfiguration.NoncurrentVersionTransition tran: rule.getNoncurrentVersionTransitions()){
			System.out.println("getBucketLifecycleConfiguration ncv tran: " + tran.getStorageClassAsString() + "," +
					tran.getDays());
		}
		if (rule.getAtimeTransitions() != null){
			for (BucketLifecycleConfiguration.AtimeTransition tran: rule.getAtimeTransitions()){
				System.out.println("getBucketLifecycleConfiguration atime tran: " + tran.getStorageClassAsString() + "," +
						tran.getDays() + "," + tran.getTransToStandard() + "," + tran.getNoTransMaxSize());
			}
		}
		if (rule.getAtimeNoncurrentVersionTransitions() != null){
			for (BucketLifecycleConfiguration.AtimeNoncurrentVersionTransition tran: rule.getAtimeNoncurrentVersionTransitions()){
				System.out.println("getBucketLifecycleConfiguration atime ncv tran: " + tran.getStorageClassAsString() + "," +
						tran.getDays() + "," + tran.getTransToStandard() + "," + tran.getNoTransMaxSize());
			}
		}
	}
}