Android生命周期

并不是所有上传至OSS的数据都需要频繁访问,但基于数据合规或者存档等原因,部分数据仍需要继续以冷存储类型进行保存。或者基于业务使用场景,希望批量删除Bucket内不再需要保存的数据。您可以配置基于最后一次修改时间(Last Modified Time)的生命周期规则,定期将Object从热存储类型转为冷存储类型或者删除Object,以降低存储成本。

注意事项

  • 要设置生命周期规则,您必须具有oss:PutBucketLifecycle权限;要查看生命周期规则,您必须具有oss:GetBucketLifecycle权限;要清空生命周期规则,您必须具有oss:DeleteBucketLifecycle权限。具体操作,请参见RAM用户授权自定义的权限策略

  • 使用本文示例前您需要先通过自定义域名、STS等方式新建OSSClient,具体请参见如何初始化AndroidOSSClient实例

设置生命周期规则

以下代码用于为examplebucket设置基于最后一次修改时间的生命周期规则。设置完成后,如果您希望修改其中的一条或多条生命周期规则,请参见如何修改其中一条或多条生命周期规则配置?

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

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

查看生命周期规则

以下代码用于查看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();

清空生命周期规则

以下代码用于清空examplebucket的所有生命周期规则。如果您需要删除其中一条或者多条生命周期规则,请参见如何删除其中一条或多条生命周期规则?

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

相关文档