All Products
Search
Document Center

Object Storage Service:Lifecycle (Java SDK)

Last Updated:Jul 31, 2025

Not all data uploaded to OSS is frequently accessed. For data compliance or archiving purposes, some data must be stored in a cold storage class. In some business scenarios, you may want to delete data in batches from a bucket when the data is no longer needed. In this case, you can configure a lifecycle rule based on the last modified time. To enable OSS to automatically monitor data access patterns, identify cold data, and then convert the storage class of the identified data to implement tiered storage and reduce storage costs, you can configure a lifecycle rule based on the last access time.

Precautions

  • In this topic, the public endpoint of the China (Hangzhou) region is used. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For details about supported regions and endpoints, see Regions and endpoints.

  • In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Configuration examples for common scenarios.

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

Set lifecycle rules

The following code provides examples of how to set lifecycle rules based on the last modified time and the last access time. After you set the lifecycle rules, if you want to modify one or more of the rules, see How do I modify one or more lifecycle rule configurations?.

Use a policy based on the last modified time to match objects by tag and prefix

The following code provides an example of how to set a lifecycle rule based on the last modified time for the bucket named examplebucket. In this example, the lifecycle rule matches objects by prefix and tag. Based on the rule, the storage classes of the matched objects are converted, or the objects are deleted.

import com.aliyun.oss.*; import com.aliyun.oss.common.auth.*; import com.aliyun.oss.common.comm.SignVersion; 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 { // The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint. String endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); // Specify the bucket name. Example: examplebucket. String bucketName = "examplebucket"; // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. String region = "cn-hangzhou"; // Create an OSSClient instance. // When the OSSClient instance is no longer used, call the shutdown method to release the resources. ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration(); clientBuilderConfiguration.setSignatureVersion(SignVersion.V4); OSS ossClient = OSSClientBuilder.create() .endpoint(endpoint) .credentialsProvider(credentialsProvider) .clientConfiguration(clientBuilderConfiguration) .region(region) .build(); try { // Create a SetBucketLifecycleRequest. SetBucketLifecycleRequest request = new SetBucketLifecycleRequest(bucketName); // Set the rule ID. String ruleId0 = "rule0"; // Set the prefix that the rule matches. String matchPrefix0 = "A0/"; // Set the tags that the rule matches. Map<String, String> matchTags0 = new HashMap<String, String>(); // Specify the key (for example, owner) and value (for example, John) of the tag to match. 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/"; // Expire objects 3 days after they are last modified. LifecycleRule rule = new LifecycleRule(ruleId0, matchPrefix0, LifecycleRule.RuleStatus.Enabled, 3); rule.setTags(matchTags0); request.AddLifecycleRule(rule); // Expire objects that are created before the specified date. rule = new LifecycleRule(ruleId1, matchPrefix1, LifecycleRule.RuleStatus.Enabled); rule.setCreatedBeforeDate(DateUtil.parseIso8601Date("2022-10-12T00:00:00.000Z")); rule.setTags(matchTags1); request.AddLifecycleRule(rule); // Expire parts 3 days after they are created. rule = new LifecycleRule(ruleId2, matchPrefix2, LifecycleRule.RuleStatus.Enabled); LifecycleRule.AbortMultipartUpload abortMultipartUpload = new LifecycleRule.AbortMultipartUpload(); abortMultipartUpload.setExpirationDays(3); rule.setAbortMultipartUpload(abortMultipartUpload); request.AddLifecycleRule(rule); // Expire parts that are created before the specified date. 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); // Convert the storage class of objects to Infrequent Access (IA) 10 days after the objects are last modified, and convert the storage class to Archive Storage 30 days after the objects are last modified. 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); // Convert the storage class of objects that are last modified before October 12, 2022 to 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 applies to buckets for which versioning is enabled. rule = new LifecycleRule(ruleId6, matchPrefix6, LifecycleRule.RuleStatus.Enabled); // Automatically convert objects to archived objects 365 days after they are last modified. storageTransitions = new ArrayList<LifecycleRule.StorageTransition>(); storageTransition = new LifecycleRule.StorageTransition(); storageTransition.setStorageClass(StorageClass.Archive); storageTransition.setExpirationDays(365); storageTransitions.add(storageTransition); rule.setStorageTransition(storageTransitions); // Automatically remove expired delete markers. rule.setExpiredDeleteMarker(true); // Convert the storage class of non-current versions of objects to Infrequent Access 10 days after the objects are last modified. LifecycleRule.NoncurrentVersionStorageTransition noncurrentVersionStorageTransition = new LifecycleRule.NoncurrentVersionStorageTransition().withNoncurrentDays(10).withStrorageClass(StorageClass.IA); // Convert the storage class of non-current versions of objects to Archive Storage 20 days after the objects are last modified. LifecycleRule.NoncurrentVersionStorageTransition noncurrentVersionStorageTransition2 = new LifecycleRule.NoncurrentVersionStorageTransition().withNoncurrentDays(20).withStrorageClass(StorageClass.Archive); // Delete non-current versions of objects after 30 days. 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); // Initiate a request to set the lifecycle rule. ossClient.setBucketLifecycle(request); // View the lifecycle rule. 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(); } } } }

Use a policy based on the last modified time to inversely match objects by tag and prefix

The following code provides an example of how to use the Not element in the filter node to convert the storage class of objects in the bucket named examplebucket to Infrequent Access (IA) 30 days after the objects are last modified. This rule applies to objects that do not have the prefix logs/not-prefix or the tag with key1 as the key and value1 as the value.

Note

You can configure the Not element only in OSS SDK for Java 3.16.0 or later.

import com.aliyun.oss.*; import com.aliyun.oss.common.auth.*; import com.aliyun.oss.common.comm.SignVersion; import com.aliyun.oss.model.*; import java.util.ArrayList; import java.util.List; public class Demo { public static void main(String[] args) throws Exception { // The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint. String endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); // Specify the bucket name. Example: examplebucket. String bucketName = "examplebucket"; // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. String region = "cn-hangzhou"; // Create an OSSClient instance. // When the OSSClient instance is no longer used, call the shutdown method to release the resources. 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("Return status code:"+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(); } } } }

Use a policy based on the last access time to convert the storage class of objects

The following code provides an example of how to set a lifecycle rule based on the last access time to convert the storage class of objects.

import com.aliyun.oss.*; import com.aliyun.oss.common.auth.*; import com.aliyun.oss.common.comm.SignVersion; import com.aliyun.oss.model.*; import java.util.ArrayList; import java.util.List; public class Demo { public static void main(String[] args) throws Exception { // The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint. String endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); // Specify the bucket name. Example: examplebucket. String bucketName = "examplebucket"; // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. String region = "cn-hangzhou"; // Create an OSSClient instance. // When the OSSClient instance is no longer used, call the shutdown method to release the resources. 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()); // Specify lifecycle rule 1. The rule specifies that all objects that have the logs prefix and are less than or equal to 64 KB in size are converted to the IA storage class 30 days after they are last accessed. When the objects that have the logs prefix are accessed again, they are still stored as IA objects. 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); // Specify lifecycle rule 2. The rule specifies that all previous versions of objects that have the dir prefix and are larger than 64 KB are converted to the IA storage class 10 days after they are last accessed. When the objects that have the dir prefix are accessed again, they are converted to the Standard storage class. 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); // Set the lifecycle rule. 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(); } } } }

View lifecycle rules

The following code provides an example of how to view the lifecycle rules for the bucket named examplebucket.

import com.aliyun.oss.*; import com.aliyun.oss.common.auth.*; import com.aliyun.oss.common.comm.SignVersion; import com.aliyun.oss.model.LifecycleRule; import java.util.List; public class Demo { public static void main(String[] args) throws Exception { // The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint. String endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); // Specify the bucket name. Example: examplebucket. String bucketName = "examplebucket"; // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. String region = "cn-hangzhou"; // Create an OSSClient instance. // When the OSSClient instance is no longer used, call the shutdown method to release the resources. ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration(); clientBuilderConfiguration.setSignatureVersion(SignVersion.V4); OSS ossClient = OSSClientBuilder.create() .endpoint(endpoint) .credentialsProvider(credentialsProvider) .clientConfiguration(clientBuilderConfiguration) .region(region) .build(); try { // Obtain the lifecycle rules. List<LifecycleRule> rules = ossClient.getBucketLifecycle(bucketName); // View the lifecycle rules. for (LifecycleRule r : rules) { System.out.println("================"); // View the rule ID. System.out.println("rule id: " + r.getId()); // View the rule status. System.out.println("rule status: " + r.getStatus()); // View the rule prefix. System.out.println("rule prefix: " + r.getPrefix()); // View the rule tags. if (r.hasTags()) { System.out.println("rule tagging: "+ r.getTags().toString()); } // View the expiration days rule. if (r.hasExpirationDays()) { System.out.println("rule expiration days: " + r.getExpirationDays()); } // View the expiration date rule. if (r.hasCreatedBeforeDate()) { System.out.println("rule expiration create before days: " + r.getCreatedBeforeDate()); } // View the rule for expired parts. 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()); } } // View the storage class transition rule. 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()); } // Check whether a lifecycle rule is configured based on the last access time. You can view this configuration only in OSS SDK for Java 3.16.0 and later. System.out.println("StorageTransition IsAccessTime: "+transition.getIsAccessTime()); // Check whether an object is converted to the Standard storage class when it is accessed again after it is converted to the IA storage class. You can view this configuration only in OSS SDK for Java 3.16.0 and later. System.out.println("StorageTransition ReturnToStdWhenVisit: "+transition.getReturnToStdWhenVisit()); } } // Check whether expired delete markers are automatically removed. if (r.hasExpiredDeleteMarker()) { System.out.println("rule expired delete marker: " + r.getExpiredDeleteMarker()); } // View the storage class transition rule for non-current versions of objects. if (r.hasNoncurrentVersionStorageTransitions()) { for (LifecycleRule.NoncurrentVersionStorageTransition transition : r.getNoncurrentVersionStorageTransitions()) { System.out.println("rule noncurrent versions trans days:" + transition.getNoncurrentDays() + " trans storage class: " + transition.getStorageClass()); // View atime. This parameter is supported only in OSS SDK for Java 3.16.0 and later. System.out.println("NoncurrentVersionStorageTransition IsAccessTime: "+transition.getIsAccessTime()); // View ReturnToStdWhenVisit. This parameter is supported only in OSS SDK for Java 3.16.0 and later. System.out.println("NoncurrentVersionStorageTransition ReturnToStdWhenVisit: "+transition.getReturnToStdWhenVisit()); } } // View the expiration rule for non-current versions of objects. 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(); } } } }

Delete all lifecycle rules

The following code provides an example of how to delete all lifecycle rules for the bucket named examplebucket. If you want to delete one or more lifecycle rules, see How do I delete one or more lifecycle rules?.

import com.aliyun.oss.*; import com.aliyun.oss.common.auth.*; import com.aliyun.oss.common.comm.SignVersion; public class Demo { public static void main(String[] args) throws Exception { // The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint. String endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); // Specify the bucket name. Example: examplebucket. String bucketName = "examplebucket"; // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. String region = "cn-hangzhou"; // Create an OSSClient instance. // When the OSSClient instance is no longer used, call the shutdown method to release the resources. 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(); } } } }

References

  • For the complete sample code for lifecycle rules, see GitHub example.

  • For more information about the API operation that you can call to set a lifecycle rule, see PutBucketLifecycle.

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

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