並不是所有上傳至OSS的資料都需要頻繁訪問,但基於資料合規或者存檔等原因,部分資料仍需要繼續以冷儲存類型進行儲存。或者基於業務使用情境,希望大量刪除Bucket內不再需要儲存的資料,您可以配置基於最後一次修改時間(Last Modified Time)的生命週期規則。如果您希望OSS自動監測資料的訪問模式並識別冷資料,然後將識別出來的冷資料進行儲存類型的轉換,從而達到資料的冷熱階層式存放區,最終降低儲存成本,您可以配置基於最後一次訪問時間(Last Access Time)的生命週期規則。
注意事項
在配置基於最後一次修改時間或者最後一次訪問時間的生命週期規則之前,請確保您已瞭解該功能。詳情請參見基於最後一次修改時間的生命週期規則和基於最後一次訪問時間的生命週期規則。
本文以華東1(杭州)外網Endpoint為例。如果您希望通過與OSS同地區的其他阿里雲產品訪問OSS,請使用內網Endpoint。關於OSS支援的Region與Endpoint的對應關係,請參見OSS訪問網域名稱、資料中心、開放連接埠。
本文以從環境變數讀取存取憑證為例。如何配置訪問憑證,請參見Java配置訪問憑證。
本文以OSS網域名稱建立OSSClient為例。如果您希望通過自訂網域名、STS等方式建立OSSClient,請參見建立OSSClient。
要設定生命週期規則,您必須有
oss:PutBucketLifecycle
許可權;要查看生命週期規則,您必須有oss:GetBucketLifecycle
許可權;要清空生命週期規則,您必須有oss:DeleteBucketLifecycle
許可權。具體操作,請參見為RAM使用者授權自訂的權限原則。
設定生命週期規則
以下代碼分別提供了設定基於最後一次修改時間和基於最後一次訪問時間的生命週期規則的樣本。設定完成後,如果您希望修改其中的一條或多條生命週期規則,請參見如何修改其中一條或多條生命週期規則配置?。
基於最後一次修改時間策略使用標籤、首碼正向匹配
以下代碼用於為examplebucket設定基於最後一次修改時間策略的生命週期規則,規則中指定按照標籤匹配和首碼匹配的方式執行轉換檔儲存類型及刪除操作。
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.utils.DateUtil;
import com.aliyun.oss.model.LifecycleRule;
import com.aliyun.oss.model.SetBucketLifecycleRequest;
import com.aliyun.oss.model.StorageClass;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Demo {
public static void main(String[] args) throws Exception {
// Endpoint以華東1(杭州)為例,其它Region請按實際情況填寫。
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// 填寫Bucket名稱,例如examplebucket。
String bucketName = "examplebucket";
// 填寫Bucket所在地區。以華東1(杭州)為例,Region填寫為cn-hangzhou。
String region = "cn-hangzhou";
// 建立OSSClient執行個體。
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// 建立SetBucketLifecycleRequest。
SetBucketLifecycleRequest request = new SetBucketLifecycleRequest(bucketName);
// 設定規則ID。
String ruleId0 = "rule0";
// 設定檔案匹配首碼。
String matchPrefix0 = "A0/";
// 設定要匹配的標籤。
Map<String, String> matchTags0 = new HashMap<String, String>();
// 依次填寫要匹配標籤的鍵(例如owner)和值(例如John)。
matchTags0.put("owner", "John");
String ruleId1 = "rule1";
String matchPrefix1 = "A1/";
Map<String, String> matchTags1 = new HashMap<String, String>();
matchTags1.put("type", "document");
String ruleId2 = "rule2";
String matchPrefix2 = "A2/";
String ruleId3 = "rule3";
String matchPrefix3 = "A3/";
String ruleId4 = "rule4";
String matchPrefix4 = "A4/";
String ruleId5 = "rule5";
String matchPrefix5 = "A5/";
String ruleId6 = "rule6";
String matchPrefix6 = "A6/";
// 距最後修改時間3天后到期。
LifecycleRule rule = new LifecycleRule(ruleId0, matchPrefix0, LifecycleRule.RuleStatus.Enabled, 3);
rule.setTags(matchTags0);
request.AddLifecycleRule(rule);
// 指定日期之前建立的檔案到期。
rule = new LifecycleRule(ruleId1, matchPrefix1, LifecycleRule.RuleStatus.Enabled);
rule.setCreatedBeforeDate(DateUtil.parseIso8601Date("2022-10-12T00:00:00.000Z"));
rule.setTags(matchTags1);
request.AddLifecycleRule(rule);
// 分區3天后到期。
rule = new LifecycleRule(ruleId2, matchPrefix2, LifecycleRule.RuleStatus.Enabled);
LifecycleRule.AbortMultipartUpload abortMultipartUpload = new LifecycleRule.AbortMultipartUpload();
abortMultipartUpload.setExpirationDays(3);
rule.setAbortMultipartUpload(abortMultipartUpload);
request.AddLifecycleRule(rule);
// 指定日期之前的分區到期。
rule = new LifecycleRule(ruleId3, matchPrefix3, LifecycleRule.RuleStatus.Enabled);
abortMultipartUpload = new LifecycleRule.AbortMultipartUpload();
abortMultipartUpload.setCreatedBeforeDate(DateUtil.parseIso8601Date("2022-10-12T00:00:00.000Z"));
rule.setAbortMultipartUpload(abortMultipartUpload);
request.AddLifecycleRule(rule);
// 距最後修改時間10天后轉低頻訪問儲存類型,距最後修改時間30天后轉Archive Storage類型。
rule = new LifecycleRule(ruleId4, matchPrefix4, LifecycleRule.RuleStatus.Enabled);
List<LifecycleRule.StorageTransition> storageTransitions = new ArrayList<LifecycleRule.StorageTransition>();
LifecycleRule.StorageTransition storageTransition = new LifecycleRule.StorageTransition();
storageTransition.setStorageClass(StorageClass.IA);
storageTransition.setExpirationDays(10);
storageTransitions.add(storageTransition);
storageTransition = new LifecycleRule.StorageTransition();
storageTransition.setStorageClass(StorageClass.Archive);
storageTransition.setExpirationDays(30);
storageTransitions.add(storageTransition);
rule.setStorageTransition(storageTransitions);
request.AddLifecycleRule(rule);
// 指定最後修改日期在2022年10月12日之前的檔案轉為Archive Storage。
rule = new LifecycleRule(ruleId5, matchPrefix5, LifecycleRule.RuleStatus.Enabled);
storageTransitions = new ArrayList<LifecycleRule.StorageTransition>();
storageTransition = new LifecycleRule.StorageTransition();
storageTransition.setCreatedBeforeDate(DateUtil.parseIso8601Date("2022-10-12T00:00:00.000Z"));
storageTransition.setStorageClass(StorageClass.Archive);
storageTransitions.add(storageTransition);
rule.setStorageTransition(storageTransitions);
request.AddLifecycleRule(rule);
// rule6針對版本控制狀態下的Bucket。
rule = new LifecycleRule(ruleId6, matchPrefix6, LifecycleRule.RuleStatus.Enabled);
// 設定Object相對最後修改時間365天之後自動轉為歸檔檔案。
storageTransitions = new ArrayList<LifecycleRule.StorageTransition>();
storageTransition = new LifecycleRule.StorageTransition();
storageTransition.setStorageClass(StorageClass.Archive);
storageTransition.setExpirationDays(365);
storageTransitions.add(storageTransition);
rule.setStorageTransition(storageTransitions);
// 設定自動移除到期刪除標記。
rule.setExpiredDeleteMarker(true);
// 設定非目前的版本的object距最後修改時間10天之後轉為低頻訪問類型。
LifecycleRule.NoncurrentVersionStorageTransition noncurrentVersionStorageTransition =
new LifecycleRule.NoncurrentVersionStorageTransition().withNoncurrentDays(10).withStrorageClass(StorageClass.IA);
// 設定非目前的版本的Object距最後修改時間20天之後轉為歸檔類型。
LifecycleRule.NoncurrentVersionStorageTransition noncurrentVersionStorageTransition2 =
new LifecycleRule.NoncurrentVersionStorageTransition().withNoncurrentDays(20).withStrorageClass(StorageClass.Archive);
// 設定非目前的版本Object 30天后刪除。
LifecycleRule.NoncurrentVersionExpiration noncurrentVersionExpiration = new LifecycleRule.NoncurrentVersionExpiration().withNoncurrentDays(30);
List<LifecycleRule.NoncurrentVersionStorageTransition> noncurrentVersionStorageTransitions = new ArrayList<LifecycleRule.NoncurrentVersionStorageTransition>();
noncurrentVersionStorageTransitions.add(noncurrentVersionStorageTransition2);
rule.setStorageTransition(storageTransitions);
rule.setNoncurrentVersionExpiration(noncurrentVersionExpiration);
rule.setNoncurrentVersionStorageTransitions(noncurrentVersionStorageTransitions);
request.AddLifecycleRule(rule);
// 發起設定生命週期規則請求。
ossClient.setBucketLifecycle(request);
// 查看生命週期規則。
List<LifecycleRule> listRules = ossClient.getBucketLifecycle(bucketName);
for(LifecycleRule rules : listRules){
System.out.println("ruleId="+rules.getId()+", matchPrefix="+rules.getPrefix());
}
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}
基於最後一次修改時間策略使用標籤、首碼反向匹配
以下代碼用於通過filter節點下的Not元素指定examplebucket中除首碼為logs/not-prefix、且包含key為key1,value為value1標籤以外的檔案在距離最後一次修改時間30天后轉低頻訪問類型。
僅3.16.0及以上版本支援配置NOT元素。
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.*;
import java.util.ArrayList;
import java.util.List;
public class Demo {
public static void main(String[] args) throws Exception {
// Endpoint以華東1(杭州)為例,其它Region請按實際情況填寫。
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// 填寫Bucket名稱,例如examplebucket。
String bucketName = "examplebucket";
// 填寫Bucket所在地區。以華東1(杭州)為例,Region填寫為cn-hangzhou。
String region = "cn-hangzhou";
// 建立OSSClient執行個體。
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
String ruleId = "mtime transition1";
String matchPrefix = "logs";
SetBucketLifecycleRequest request = new SetBucketLifecycleRequest(bucketName);
LifecycleFilter filter = new LifecycleFilter();
LifecycleNot not = new LifecycleNot();
Tag tag = new Tag("key1","value1");
not.setPrefix("logs/not-prefix");
not.setTag(tag);
List<LifecycleNot> notList = new ArrayList<LifecycleNot>();
notList.add(not);
filter.setNotList(notList);
List<LifecycleRule.StorageTransition> storageTransitions = new ArrayList<LifecycleRule.StorageTransition>();
LifecycleRule.StorageTransition storageTransition = new LifecycleRule.StorageTransition();
storageTransition.setStorageClass(StorageClass.IA);
storageTransition.setExpirationDays(30);
storageTransitions.add(storageTransition);
LifecycleRule rule = new LifecycleRule(ruleId, matchPrefix, LifecycleRule.RuleStatus.Enabled);
rule.setFilter(filter);
rule.setStorageTransition(storageTransitions);
request.AddLifecycleRule(rule);
VoidResult result = ossClient.setBucketLifecycle(request);
System.out.println("返回狀態代碼:"+result.getResponse().getStatusCode()+" set lifecycle succeed");
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}
基於最後一次訪問時間策略轉換檔儲存類型
以下代碼用於為examplebucket設定基於最後一次訪問時間策略執行轉換儲存類型的操作。
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.*;
import java.util.ArrayList;
import java.util.List;
public class Demo {
public static void main(String[] args) throws Exception {
// Endpoint以華東1(杭州)為例,其它Region請按實際情況填寫。
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// 填寫Bucket名稱,例如examplebucket。
String bucketName = "examplebucket";
// 填寫Bucket所在地區。以華東1(杭州)為例,Region填寫為cn-hangzhou。
String region = "cn-hangzhou";
// 建立OSSClient執行個體。
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
ossClient.putBucketAccessMonitor(bucketName, AccessMonitor.AccessMonitorStatus.Enabled.toString());
// 指定生命週期規則1。規則中指定首碼為logs,且小於等於64 KB的所有檔案在距離最後一次訪問時間30天后轉為低頻訪問類型。且再次訪問首碼為logs的檔案時,這些檔案仍保留為低頻訪問類型。
LifecycleRule lifecycleRule = new LifecycleRule("rule1", "logs", LifecycleRule.RuleStatus.Enabled);
List<LifecycleRule> lifecycleRuleList = new ArrayList<LifecycleRule>();
SetBucketLifecycleRequest setBucketLifecycleRequest = new SetBucketLifecycleRequest(bucketName);
LifecycleRule.StorageTransition storageTransition = new LifecycleRule.StorageTransition();
storageTransition.setStorageClass(StorageClass.IA);
storageTransition.setExpirationDays(30);
storageTransition.setIsAccessTime(true);
storageTransition.setReturnToStdWhenVisit(false);
storageTransition.setAllowSmallFile(true);
List<LifecycleRule.StorageTransition> storageTransitionList = new ArrayList<LifecycleRule.StorageTransition>();
storageTransitionList.add(storageTransition);
lifecycleRule.setStorageTransition(storageTransitionList);
lifecycleRuleList.add(lifecycleRule);
// 指定生命週期規則2。規則中指定首碼為dir,且大於64 KB的所有歷史版本檔案在距離最後一次訪問時間10天后轉為低頻訪問類型。且再次訪問首碼為dir的檔案時,這些檔案將轉為標準儲存。
LifecycleRule lifecycleRule2 = new LifecycleRule("rule2", "dir", LifecycleRule.RuleStatus.Enabled);
LifecycleRule.NoncurrentVersionStorageTransition noncurrentVersionStorageTransition = new LifecycleRule.NoncurrentVersionStorageTransition();
noncurrentVersionStorageTransition.setStorageClass(StorageClass.IA);
noncurrentVersionStorageTransition.setNoncurrentDays(10);
noncurrentVersionStorageTransition.setIsAccessTime(true);
noncurrentVersionStorageTransition.setReturnToStdWhenVisit(true);
noncurrentVersionStorageTransition.setAllowSmallFile(false);
List<LifecycleRule.NoncurrentVersionStorageTransition> noncurrentVersionStorageTransitionList = new ArrayList<LifecycleRule.NoncurrentVersionStorageTransition>();
noncurrentVersionStorageTransitionList.add(noncurrentVersionStorageTransition);
lifecycleRule2.setNoncurrentVersionStorageTransitions(noncurrentVersionStorageTransitionList);
lifecycleRuleList.add(lifecycleRule2);
setBucketLifecycleRequest.setLifecycleRules(lifecycleRuleList);
// 設定生命週期規則。
ossClient.setBucketLifecycle(setBucketLifecycleRequest);
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}
查看生命週期規則
以下代碼用於查看examplebucket的生命週期規則。
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.LifecycleRule;
import java.util.List;
public class Demo {
public static void main(String[] args) throws Exception {
// Endpoint以華東1(杭州)為例,其它Region請按實際情況填寫。
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// 填寫Bucket名稱,例如examplebucket。
String bucketName = "examplebucket";
// 填寫Bucket所在地區。以華東1(杭州)為例,Region填寫為cn-hangzhou。
String region = "cn-hangzhou";
// 建立OSSClient執行個體。
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// 擷取生命週期規則。
List<LifecycleRule> rules = ossClient.getBucketLifecycle(bucketName);
// 查看生命週期規則。
for (LifecycleRule r : rules) {
System.out.println("================");
// 查看規則ID。
System.out.println("rule id: " + r.getId());
// 查看規則狀態。
System.out.println("rule status: " + r.getStatus());
// 查看規則首碼。
System.out.println("rule prefix: " + r.getPrefix());
// 查看規則標籤。
if (r.hasTags()) {
System.out.println("rule tagging: "+ r.getTags().toString());
}
// 查看到期天數規則。
if (r.hasExpirationDays()) {
System.out.println("rule expiration days: " + r.getExpirationDays());
}
// 查看到期日期規則。
if (r.hasCreatedBeforeDate()) {
System.out.println("rule expiration create before days: " + r.getCreatedBeforeDate());
}
// 查看到期分區規則。
if(r.hasAbortMultipartUpload()) {
if(r.getAbortMultipartUpload().hasExpirationDays()) {
System.out.println("rule abort uppart days: " + r.getAbortMultipartUpload().getExpirationDays());
}
if (r.getAbortMultipartUpload().hasCreatedBeforeDate()) {
System.out.println("rule abort uppart create before date: " + r.getAbortMultipartUpload().getCreatedBeforeDate());
}
}
// 查看儲存類型轉換規則。
if (r.getStorageTransition().size() > 0) {
for (LifecycleRule.StorageTransition transition : r.getStorageTransition()) {
if (transition.hasExpirationDays()) {
System.out.println("rule storage trans days: " + transition.getExpirationDays() +
" trans storage class: " + transition.getStorageClass());
}
if (transition.hasCreatedBeforeDate()) {
System.out.println("rule storage trans before create date: " + transition.getCreatedBeforeDate());
}
// 查看是否基於最後一次訪問時間配置了生命週期規則。僅Java SDK 3.16.0及以上版本支援查看。
System.out.println("StorageTransition IsAccessTime: "+transition.getIsAccessTime());
// 查看Object轉為低頻訪問類型後,再次訪問時Object是否轉為標準儲存類型。僅Java SDK 3.16.0及以上版本支援查看。
System.out.println("StorageTransition ReturnToStdWhenVisit: "+transition.getReturnToStdWhenVisit());
}
}
// 查看是否自動刪除到期刪除標記。
if (r.hasExpiredDeleteMarker()) {
System.out.println("rule expired delete marker: " + r.getExpiredDeleteMarker());
}
// 查看非目前的版本Object儲存類型轉換規則。
if (r.hasNoncurrentVersionStorageTransitions()) {
for (LifecycleRule.NoncurrentVersionStorageTransition transition : r.getNoncurrentVersionStorageTransitions()) {
System.out.println("rule noncurrent versions trans days:" + transition.getNoncurrentDays() +
" trans storage class: " + transition.getStorageClass());
// 查看atime。 僅Java SDK 3.16.0及以上版本支援。
System.out.println("NoncurrentVersionStorageTransition IsAccessTime: "+transition.getIsAccessTime());
// 查看ReturnToStdWhenVisit。 僅Java SDK 3.16.0及以上版本支援。
System.out.println("NoncurrentVersionStorageTransition ReturnToStdWhenVisit: "+transition.getReturnToStdWhenVisit());
}
}
// 查看非目前的版本Object到期規則。
if (r.hasNoncurrentVersionExpiration()) {
System.out.println("rule noncurrent versions expiration days:" + r.getNoncurrentVersionExpiration().getNoncurrentDays());
}
}
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}
清空生命週期規則
以下代碼用於清空examplebucket的所有生命週期規則。如果您需要刪除其中一條或者多條生命週期規則,請參見如何刪除其中一條或多條生命週期規則?。
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
public class Demo {
public static void main(String[] args) throws Exception {
// Endpoint以華東1(杭州)為例,其它Region請按實際情況填寫。
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// 填寫Bucket名稱,例如examplebucket。
String bucketName = "examplebucket";
// 填寫Bucket所在地區。以華東1(杭州)為例,Region填寫為cn-hangzhou。
String region = "cn-hangzhou";
// 建立OSSClient執行個體。
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
ossClient.deleteBucketLifecycle(bucketName);
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}
相關文檔
關於生命週期的完整範例程式碼,請參見GitHub樣本。
關於設定生命週期規則的API介面說明,請參見PutBucketLifecycle。
關於查看生命週期規則的API介面說明,請參見GetBucketLifecycle。
關於清空生命週期規則的API介面說明,請參見DeleteBucketLifecycle。