All Products
Search
Document Center

Object Storage Service:Lifecycle rules

Last Updated:Mar 11, 2024

Some data uploaded to Object Storage Service (OSS) may not be frequently accessed but still needs to be stored in cold storage due to compliance or archiving requirements. You may want to delete data that is no longer required in batches to reduce storage costs. In this case, you can configure lifecycle rules based on the last modified time of objects to periodically change the storage class of objects from hot to cold or delete objects to reduce storage costs.

Usage notes

  • To configure lifecycle rules, you must have the oss:PutBucketLifecycle permission. To query lifecycle rules, you must have the oss:GetBucketLifecycle permission. To delete lifecycle rules, you must have the oss:DeleteBucketLifecycle permission. For more information, see Attach a custom policy to a RAM user.

  • Before you run the sample code in this topic, you must create an OSSClient instance by using methods such as using a custom domain name or Security Token Service (STS). For more information, see Initialization.

Configure lifecycle rules for a bucket

The following sample code provides an example on how to configure lifecycle rules based on the last modified time of objects for a bucket named examplebucket. To modify lifecycle rules, follow the instructions as described in How do I change the configurations of one or more lifecycle rules?

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

BucketLifecycleRule rule1 = new BucketLifecycleRule();
// Specify the rule ID and the prefix contained in the names of the objects that match the rule. 
rule1.setIdentifier("1");
rule1.setPrefix("A");
// Specify whether to run the lifecycle rule. If this parameter is set to true, OSS periodically runs this rule. 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 classes of objects are 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 classes of objects are converted to Infrequent Access (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();

Query the lifecycle rules of a bucket

The following code provides an example on how to query the lifecycle rules configured for the bucket named examplebucket:

GetBucketLifecycleRequest request = new GetBucketLifecycleRequest();
request.setBucketName("examplebucket");
OSSAsyncTask task = oss.asyncGetBucketLifecycle(request, new OSSCompletedCallback<GetBucketLifecycleRequest, GetBucketLifecycleResult>() {
    @Override
    public void onSuccess(GetBucketLifecycleRequest request, GetBucketLifecycleResult result) {
         ArrayListBucketLifecycleRule> 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();

Delete the lifecycle rules of a bucket

The following code provides an example on how to clear lifecycle rules in a bucket named examplebucket. If you want to delete one or more lifecycle rules, refer to How do I delete one or more lifecycle rules that are configured for a bucket?.

DeleteBucketLifecycleRequest request = new DeleteBucketLifecycleRequest();
request.setBucketName("examplebucket");
OSSAsyncTask task = oss.asyncDeleteBucketLifecycle(request,"examplebucket" 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();

References

  • For the complete sample code that is used to manage lifecycle rules, visit GitHub.

  • For more information about the API operation that you can call to configure lifecycle rules, see PutBucketLifecycle.

  • For more information about the API operation that you can call to query lifecycle rules, see GetBucketLifecycle.

  • For more information about the API operation that you can call to delete lifecycle rules, see DeleteBucketLifecycle.

  • For more information about how to initialize an OSSClient instance, see Initialization.