• Object Storage Service

obs
  1. Help Center
  2. Object Storage Service
  3. Developer Guide (Android 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 being created.
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.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 being created.
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 prfix will expire 60 days after changing into nocurrent versions.
noncurrentVersionExpiration.setDays(60);
rule.setNoncurrentVersionExpiration(noncurrentVersionExpiration);
config.addRule(rule);

obsClient.setBucketLifecycleConfiguration("bucketname", config);

// Close obsClient.
obsClient.close();