This document provides an overview of APIs and SDK code samples related to lifecycle.
API | Operation | Description |
---|---|---|
PUT Bucket lifecycle | Setting lifecycle configuration | Sets lifecycle management configuration for a bucket |
GET Bucket lifecycle | Querying a lifecycle configuration | Queries the lifecycle management configuration of a bucket |
DELETE Bucket lifecycle | Deleting lifecycle | Deletes the lifecycle management configuration of bucket |
This API is used to set the lifecycle configuration for a bucket.
public void setBucketLifecycleConfiguration(String bucketName, BucketLifecycleConfiguration bucketLifecycleConfiguration)
throws CosClientException, CosServiceException;
List<BucketLifecycleConfiguration.Rule> rules = new ArrayList<BucketLifecycleConfiguration.Rule>();
// Rule 1: delete files whose paths start with hongkong_movie/ after 30 days
BucketLifecycleConfiguration.Rule deletePrefixRule = new BucketLifecycleConfiguration.Rule();
deletePrefixRule.setId("delete prefix xxxy after 30 days");
deletePrefixRule.setFilter(new LifecycleFilter(new LifecyclePrefixPredicate("hongkong_movie/")));
// Deleting files that have been uploaded or modified for 30 days
deletePrefixRule.setExpirationInDays(30);
// Setting rules to active
deletePrefixRule.setStatus(BucketLifecycleConfiguration.ENABLED);
// Rule 2: put files into low frequency after 20 days and delete them after one year
BucketLifecycleConfiguration.Rule standardIaRule = new BucketLifecycleConfiguration.Rule();
standardIaRule.setId("standard_ia transition");
standardIaRule.setFilter(new LifecycleFilter(new LifecyclePrefixPredicate("standard_ia/")));
List<BucketLifecycleConfiguration.Transition> standardIaTransitions = new ArrayList<BucketLifecycleConfiguration.Transition>();
BucketLifecycleConfiguration.Transition standardTransition = new BucketLifecycleConfiguration.Transition();
standardTransition.setDays(20);
standardTransition.setStorageClass(StorageClass.Standard_IA.toString());
standardIaTransitions.add(standardTransition);
standardIaRule.setTransitions(standardIaTransitions);
standardIaRule.setStatus(BucketLifecycleConfiguration.ENABLED);
standardIaRule.setExpirationInDays(365);
// Adding two rules to the policy set
rules.add(deletePrefixRule);
rules.add(standardIaRule);
// Generating bucketLifecycleConfiguration
BucketLifecycleConfiguration bucketLifecycleConfiguration =
new BucketLifecycleConfiguration();
bucketLifecycleConfiguration.setRules(rules);
// Bucket name in the format of BucketName-APPID
String bucketName = "examplebucket-1250000000";
SetBucketLifecycleConfigurationRequest setBucketLifecycleConfigurationRequest =
new SetBucketLifecycleConfigurationRequest(bucketName, bucketLifecycleConfiguration);
// Configuring the lifecycle configuration
cosClient.setBucketLifecycleConfiguration(setBucketLifecycleConfigurationRequest);
Parameter | Description | Type |
---|---|---|
bucketName | Bucket name in the format of BucketName-APPID . For more information, please see Naming Conventions |
String |
bucketLifecycleConfiguration | Lifecycle configuration | BucketLifecycleConfiguration |
CosClientException
or CosServiceException
exception thrown. For more information, please see Troubleshooting.This API is used to query the lifecycle management configuration for a bucket.
public BucketLifecycleConfiguration getBucketLifecycleConfiguration(String bucketName)
throws CosClientException, CosServiceException;
// Entering the bucket name in the format of BucketName-APPID.
String bucketName = "examplebucket-1250000000";
BucketLifecycleConfiguration queryLifeCycleRet =
cosClient.getBucketLifecycleConfiguration(bucketName);
List<BucketLifecycleConfiguration.Rule> ruleLists = queryLifeCycleRet.getRules();
Parameter | Description | Type |
---|---|---|
bucketName | Bucket name in the format of BucketName-APPID . For more information, please see Naming Conventions |
String |
BucketLifecycleConfiguration
class is returned, which contains the lifecycle rule of the bucket.CosClientException
or CosServiceException
exception thrown. For more information, please see Troubleshooting.This API is used to delete the lifecycle configuration of a Bucket.
public void deleteBucketLifecycleConfiguration(String bucketName)
throws CosClientException, CosServiceException;
// Bucket name in the format of BucketName-APPID
String bucketName = "examplebucket-1250000000";
cosClient.deleteBucketLifecycleConfiguration(bucketName);
Parameter | Description | Type |
---|---|---|
bucketName | Bucket name in the format of BucketName-APPID . For more information, please see Naming Conventions |
String |
CosClientException
or CosServiceException
exception thrown. For more information, please see Troubleshooting.
Was this page helpful?