全部產品
Search
文件中心

Object Storage Service:Android生命週期

更新時間:Jun 19, 2024

並不是所有上傳至OSS的資料都需要頻繁訪問,但基於資料合規或者存檔等原因,部分資料仍需要繼續以冷儲存類型進行儲存。或者基於業務使用情境,希望大量刪除Bucket內不再需要儲存的資料。您可以配置基於最後一次修改時間(Last Modified Time)的生命週期規則,定期將Object從熱儲存類型轉為冷儲存類型或者刪除Object,以降低儲存成本。

注意事項

  • 要設定生命週期規則,您必須具有oss:PutBucketLifecycle許可權;要查看生命週期規則,您必須具有oss:GetBucketLifecycle許可權;要清空生命週期規則,您必須具有oss:DeleteBucketLifecycle許可權。具體操作,請參見為RAM使用者授權自訂的權限原則

  • 使用本文樣本前您需要先通過自訂網域名、STS等方式建立OSSClient,具體請參見如何初始化Android端OSSClient執行個體

設定生命週期規則

以下代碼用於為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 Storage類型(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();

相關文檔