Setting Lifecycle Rules

If you have any questions during development, post them on the Issues page of GitHub. For details about parameters and usage of each API, see the API Reference

You can call ObsClient.setBucketLifecycle to set lifecycle rules for a bucket.

Setting an Object Transition Policy

Sample code:

String endPoint = "https://your-endpoint";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";

// Create an instance of ObsClient.
ObsClient obsClient = new ObsClient(ak, sk, endPoint);

LifecycleConfiguration config = new LifecycleConfiguration();
Rule rule = config.new Rule();
rule.setEnabled(true);
rule.setId("rule1");
rule.setPrefix("prefix");
Transition transition = config.new Transition();
// Specify that objects whose names contain the prefix will be transited 30 days after creation.
transition.setDays(30);
// Specify the storage class of the objects after transition.
transition.setObjectStorageClass(StorageClassEnum.STANDARD_IA);
// Specify a date when the objects whose names contain the prefix will be transited.
// transition.setDate(new SimpleDateFormat("yyyy-MM-dd").parse("2018-10-31")); 
rule.getTransitions().add(transition);

NoncurrentVersionTransition noncurrentVersionTransition = config.new NoncurrentVersionTransition();
// Specify that objects whose names contain the prefix will be transited 30 days after changing into noncurrent versions.
noncurrentVersionTransition.setDays(30);
// Specify the storage class of the noncurrent object version after transition.
noncurrentVersionTransition.setObjectStorageClass(StorageClassEnum.COLD);
rule.getNoncurrentVersionTransitions().add(noncurrentVersionTransition);

config.addRule(rule);

obsClient.setBucketLifecycle("bucketname", config);

Setting an Object Expiration Time

Sample code:

String endPoint = "https://your-endpoint";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";
// Create an instance of ObsClient.
ObsClient obsClient = new ObsClient(ak, sk, endPoint);

LifecycleConfiguration config = new LifecycleConfiguration();

Rule rule = config.new Rule();
rule.setEnabled(true);
rule.setId("rule1");
rule.setPrefix("prefix");
Expiration expiration = config.new Expiration();
// Specify that objects whose names contain the prefix will expire 60 days after creation.
expiration.setDays(60);
// Specify a date when the objects whose names contain the prefix will expire.
// expiration.setDate(new SimpleDateFormat("yyyy-MM-dd").parse("2018-12-31")); 
rule.setExpiration(expiration);

NoncurrentVersionExpiration noncurrentVersionExpiration = config.new NoncurrentVersionExpiration();
// Specify that objects whose names contain the prefix will expire 60 days after changing into noncurrent versions.
noncurrentVersionExpiration.setDays(60);
rule.setNoncurrentVersionExpiration(noncurrentVersionExpiration);
config.addRule(rule);

obsClient.setBucketLifecycle("bucketname", config);