生命周期管理

MSS允许用户对Bucket设置生命周期规则,以自动淘汰过期掉的文件,节省存储空间。针对不同前缀的文件,用户可以同时设置多条规则。一条规则包含:

  • 规则ID,用于标识一条规则,不能重复
  • 受影响的文件前缀,此规则只作用于符合前缀的文件
  • 过期时间,有三种指定方式:
    (1)指定距文件最后修改时间N天过期
    (2)指定日期创建前的文件过期,之后的不过期
    (3)指定在具体的某一天过期,即在那天之后符合前缀的文件将会过期,而不论文件的最后修改时间。不推荐使用。
  • 是否生效

上面的过期规则对用户上传的文件有效。用户通过uploadPart上传的分片,也可以设置过期规则。Multipart的Lifecycle和文件的类似,过期时间支持1、2两种,不支持3,生效是以init Multipart upload的时间为准。

设置生命周期规则

public void putBucketLifecycleExample(String bucketName){
    try{
        BucketLifecycleConfiguration lifecycleConfig = new BucketLifecycleConfiguration();
        List<Rule> lifecycleRules = new ArrayList<Rule>();
        //rule 设置bucket下photo前缀的文件在60天后过期
        Rule ruleOne = new Rule().withId("id1").withExpirationInDays(600).withPrefix("photo").withStatus("Enabled");
        lifecycleRules.add(ruleOne);
        //rule 设置特定日期过期
        Rule ruleTwo = new Rule().withId("id2").withExpirationDate(DateUtils.parseISO8601Date("2022-10-12T00:00:00.000Z")).withPrefix("vedio").withStatus("Enabled");
        lifecycleRules.add(ruleTwo);
        lifecycleConfig.setRules(lifecycleRules);

        s3client.setBucketLifecycleConfiguration(bucketName,lifecycleConfig);
    }catch (AmazonServiceException ase) {
        //存储服务端处理异常
        System.out.println("Caught an ServiceException.");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
    }catch (AmazonClientException ace) {
        //客户端处理异常
        System.out.println("Caught an ClientException.");
        System.out.println("Error Message: " + ace.getMessage());
    }
}

查看生命周期规则

public void getBucketLifecycleExample(String bucketName){
    try{
        BucketLifecycleConfiguration config = s3client.getBucketLifecycleConfiguration(bucketName);
        if (config != null){
            for (Rule rule : config.getRules()) {
                System.out.println(rule.getId());
                System.out.println(rule.getPrefix());
                System.out.println(rule.getExpirationInDays());
                System.out.println(rule.getExpirationDate());
            }
        }
    }catch (AmazonServiceException ase) {
        //存储服务端处理异常
        System.out.println("Caught an ServiceException.");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
    }catch (AmazonClientException ace) {
        //客户端处理异常
        System.out.println("Caught an ClientException.");
        System.out.println("Error Message: " + ace.getMessage());
    }
}

清空生命周期规则

public void deleteBucketLifecycleExample(String bucketName){
    try{
        s3client.deleteBucketLifecycleConfiguration(bucketName);
    }catch (AmazonServiceException ase) {
        //存储服务端处理异常
        System.out.println("Caught an ServiceException.");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
    }catch (AmazonClientException ace) {
        //客户端处理异常
        System.out.println("Caught an ClientException.");
        System.out.println("Error Message: " + ace.getMessage());
    }
}