• Object Storage Service

obs
  1. Help Center
  2. Object Storage Service
  3. Developer Guide (Node.js 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:
// Import the OBS library.
var ObsClient = require('./lib/obs');

// Create an instance of ObsClient.
var obsClient = new ObsClient({
    access_key_id: '*** Provide your Access Key ***',       
    secret_access_key: '*** Provide your Secret Key ***',       
    server : 'yourdomainname'
});

obsClient.setBucketLifecycleConfiguration({
       Bucket: 'bucketname',
       Rules:[
              {
                     ID:'rule1',Prefix:'prefix1',Status:'Enabled',
                     
                      // Specify that objects whose names contain the prefix will be transited to OBS Warm 30 days after being created.
                     Transitions:[{StorageClass: obsClient.enums.StorageClassWarm, Days:30}],
                     
                     // Specify that objects whose names contain the prefix will be transited to OBS Cold 30 days after becoming noncurrent versions.
                     NoncurrentVersionTransitions:[{StorageClass: obsClient.enums.StorageClassCold, NoncurrentDays : 30}]
              },
              {
                     ID:'rule2',Prefix:'prefix2',Status:'Enabled',
                     
                     // Specify the date when objects whose names contain the prefix will be transited to OBS Warm.
                     Transitions:[{StorageClass: obsClient.enums.StorageClassWarm, Date: '2018-10-31T00:00:00Z'}],
              }
       ]
}, (err, result) => {
       if(err){
              console.log('Error-->' + err);
       }else{
              console.log('Status-->' + result.CommonMsg.Status);
       }
});

Setting an Object Expiration Time

Sample code:

// Import the OBS library.
var ObsClient = require('./lib/obs');

// Create an instance of ObsClient.
var obsClient = new ObsClient({
    access_key_id: '*** Provide your Access Key ***',       
    secret_access_key: '*** Provide your Secret Key ***',       
    server : 'yourdomainname'
});

obsClient.setBucketLifecycleConfiguration({
       Bucket: 'bucketname',
       Rules:[
              {
                     ID:'rule1',Prefix:'prefix1',Status:'Enabled',
                      // Specify that objects whose names contain the prefix will expire 60 days after being created.
                     Expiration:{Days:60},
                      // Specify that objects whose names contain the prefix will expire 60 days after becoming noncurrent versions.
                     NoncurrentVersionExpiration:{NoncurrentDays : 60}
              },
              {
                     ID:'rule2',Prefix:'prefix2',Status:'Enabled',
// Directly set the time when objects whose names contain the prefix will expire. The value must conform to the ISO8601 standards and must be at 00:00 (UTC time). 
                     Expiration:{Date: '2018-12-31T00:00:00Z'},
              }
       ]
}, (err, result) => {
       if(err){
              console.log('Error-->' + err);
       }else{
              console.log('Status-->' + result.CommonMsg.Status);
       }
});
NOTE:

Use the Rules parameter to specify the lifecycle rules for a bucket.