This topic describes how to manage lifecycle rules.

Configure lifecycle rules

The following code provides an example on how to configure lifecycle rules:

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

BucketLifecycleRule rule1 = new BucketLifecycleRule();
// Specify the rule ID and the object prefix to match the rule.
rule1.setIdentifier("1");
rule1.setPrefix("A");
//Specify whether to run the lifecycle rule. If this parameter is set to true, OSS runs this rule at specific intervals. If this parameter is set to false, OSS ignores this rule.
rule1.setStatus(true);
// Specify that objects expire 200 days after they are last modified.
rule1.setDays("200");
// Specify that the storage class of objects is converted to Archive 30 days after they are last modified. 
rule1.setArchiveDays("30");
// Specify that parts expire three days after they fail to be uploaded.
rule1.setMultipartDays("3");
// Specify that the storage class of objects is converted to IA 15 days after they are last modified. 
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();

For more information, see PutBucketLifecycle.

View lifecycle rules

The following code provides an example on how to view lifecycle rules:

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();

For more information, see GetBucketLifecycle.

Delete lifecycle rules

The following code provides an example on how to clear lifecycle rules:

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();

For more information, see DeleteBucketLifecycle.