OSS支持设置生命周期(Lifecycle)规则,自动删除过期的文件和碎片,或将到期的文件转储为低频或归档存储类型,从而节省存储费用。本文介绍如何管理生命周期规则。

背景信息

每条生命周期规则包含以下内容:

  • 生命周期规则ID。用于标识一条规则,同一存储空间内规则ID不能重复。
  • 生命周期策略。有以下两种设置方式。同一存储空间内仅支持一种设置方式。
    • 按前缀匹配。此种方式允许创建多条规则,前缀不能重复。
    • 配置到整个存储空间。此种方式只能创建一条规则。
  • 指定文件过期时间。有两种指定方式:
    • 指定一个过期天数N,文件会在其最近更新时间点的N天后过期。
    • 指定一个过期时间点,最近更新时间在该时间点之前的文件全部过期。
  • 生命周期规则是否生效。

通过uploadPart方法上传的分片也支持设置生命周期规则。文件最后修改时间以初始化分片上传事件的时间为准。

更多关于生命周期的内容请参见管理对象生命周期

设置生命周期规则

以下代码用于设置生命周期规则:

PutBucketLifecycleRequest request = new PutBucketLifecycleRequest();
request.setBucketName("yourBucketName");

BucketLifecycleRule rule1 = new BucketLifecycleRule();
// 设置规则ID和文件前缀。
rule1.setIdentifier("1");
rule1.setPrefix("A");
//设置是否执行生命周期规则。如果值为true,则OSS会定期执行该规则;如果值为false,则OSS会忽略该规则。
rule1.setStatus(true);
//距最后修改时间200天后过期。
rule1.setDays("200");
//30天后自动转为归档存储类型(Archive)
rule1.setArchiveDays("30");
//未完成分片3天后过期。
rule1.setMultipartDays("3");
//15天后自动转为低频存储类型(IA)。
rule1.setIADays("15");

BucketLifecycleRule rule2 = new BucketLifecycleRule();
rule2.setIdentifier("2");
rule2.setPrefix("B");
rule2.setStatus(true);
rule2.setDays("300");
rule2.setArchiveDays("30");
rule2.setMultipartDays("3");
rule2.setIADays("15");

ArrayList<BucketLifecycleRule> lifecycleRules = new ArrayList<BucketLifecycleRule>();
lifecycleRules.add(rule1);
lifecycleRules.add(rule2);
request.setLifecycleRules(lifecycleRules);
OSSAsyncTask task = oss.asyncPutBucketLifecycle(request, new OSSCompletedCallback<PutBucketLifecycleRequest, PutBucketLifecycleResult>() {
    @Override
    public void onSuccess(PutBucketLifecycleRequest request, PutBucketLifecycleResult result) {
        OSSLog.logInfo("code::"+result.getStatusCode());

    }

    @Override
    public void onFailure(PutBucketLifecycleRequest request, ClientException clientException, ServiceException serviceException) {
        OSSLog.logError("error: "+serviceException.getRawMessage());

    }
});

task.waitUntilFinished();

有关设置生命周期的更多信息,请参见PutBucketLifecycle

查看生命周期规则

以下代码用于查看生命周期规则:

GetBucketLifecycleRequest request = new GetBucketLifecycleRequest();
request.setBucketName("yourBucketName");
OSSAsyncTask task = oss.asyncGetBucketLifecycle(request, new OSSCompletedCallback<GetBucketLifecycleRequest, GetBucketLifecycleResult>() {
    @Override
    public void onSuccess(GetBucketLifecycleRequest request, GetBucketLifecycleResult result) {
         ArrayList<BucketLifecycleRule> list = result.getlifecycleRules();
         for (BucketLifecycleRule rule : list){
             OSSLog.logInfo("info: " + rule.getIdentifier());
         }
    }

    @Override
    public void onFailure(GetBucketLifecycleRequest request, ClientException clientException, ServiceException serviceException) {
        OSSLog.logError("error: "+serviceException.getRawMessage());

    }
});
task.waitUntilFinished();

有关查看生命周期的更多信息,请参见GetBucketLifecycle

清空生命周期规则

以下代码用于清空生命周期规则:

DeleteBucketLifecycleRequest request = new DeleteBucketLifecycleRequest();
request.setBucketName("yourBucketName");
OSSAsyncTask task = oss.asyncDeleteBucketLifecycle(request,"yourBucketName" new OSSCompletedCallback<DeleteBucketLifecycleRequest, DeleteBucketLifecycleResult>() {
    @Override
    public void onSuccess(DeleteBucketLifecycleRequest request, DeleteBucketLifecycleResult result) {
        OSSLog.logInfo("code :  "+result.getStatusCode());

    }

    @Override
    public void onFailure(DeleteBucketLifecycleRequest request, ClientException clientException, ServiceException serviceException) {
        OSSLog.logError("error: "+serviceException.getRawMessage());

    }
});
task.waitUntilFinished();

有关清空生命周期规则的更多信息,请参见DeleteBucketLifecycle