Updated on 2024-04-26 GMT+08:00

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:
// Create an instance of ObsClient.
var obsClient = new ObsClient({
    // Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables AccessKeyID and SecretAccessKey.
    // The front-end code does not have the process environment variable, so you need to use a module bundler like webpack to define the process variable.
    // Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
    access_key_id: process.env.AccessKeyID,
    secret_access_key: process.env.SecretAccessKey,
    // Replace the example endpoint with the actual one in your case.
    server: 'https://obs.ap-southeast-1.myhuaweicloud.com'
});

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

Setting an Object Expiration Time

Sample code:

// Create an instance of ObsClient.
var obsClient = new ObsClient({
    // Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables AccessKeyID and SecretAccessKey.
    // The front-end code does not have the process environment variable, so you need to use a module bundler like webpack to define the process variable.
    // Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
    access_key_id: process.env.AccessKeyID,
    secret_access_key: process.env.SecretAccessKey,
    // Replace the example endpoint with the actual one in your case.
    server: 'https://obs.ap-southeast-1.myhuaweicloud.com'
});

obsClient.setBucketLifecycle({
       Bucket: 'bucketname',
       Rules:[
              {
                     ID:'rule1',Prefix:'prefix1',Status:'Enabled',
                      // Specify that objects whose names contain the specified prefix will expire 60 days after creation.
                     Expiration:{Days:60},
                      // Specify that objects whose names contain the specified prefix will expire after changing into noncurrent versions for 60 days.
                     NoncurrentVersionExpiration:{NoncurrentDays : 60}
              },
              {
                     ID:'rule2',Prefix:'prefix2',Status:'Enabled',
                     // Specify when the objects whose names contain the specified 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'},
              }
       ]
}, function (err, result) {
       if(err){
              console.log('Error-->' + err);
       }else{
              console.log('Status-->' + result.CommonMsg.Status);
       }
});

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