• Object Storage Service

obs
  1. Help Center
  2. Object Storage Service
  3. Developer Guide (Java SDK)
  4. Lifecycle Management
  5. Setting Lifecycle Rules

Setting Lifecycle Rules

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

Setting an Object Transition Policy

Sample code:

String endPoint = "yourdomainname";
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 object after transition.
transition.setObjectStorageClass(StorageClassEnum.WARM);
// 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 becoming 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.setBucketLifecycleConfiguration("bucketname", config);

// Close obsClient.
obsClient.close();

Setting an Object Expiration Time

Sample code:

String endPoint = "yourdomainname";
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 20 days after becoming noncurrent versions.
noncurrentVersionExpiration.setDays(20);
rule.setNoncurrentVersionExpiration(noncurrentVersionExpiration);
config.addRule(rule);

obsClient.setBucketLifecycleConfiguration("bucketname", config);

// Close obsClient.
obsClient.close();