This topic describes how to manage lifecycle rules.

Configure lifecycle rules

The following code provides an example on how to use putBucketLifecycle to configure lifecycle rules for a bucket:

const OSS = require('ali-oss')
const client = new OSS({
  // The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint.
  region: '<Your region>'
  // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
  accessKeyId: '<Your AccessKeyId>',
  accessKeySecret: '<Your AccessKeySecret>',
  bucket: '<Your bucket name>'
});

async function putBucketLifecycle(lifecycle) {
  try {
    const result = await client.putBucketLifecycle('<bucket-name>', [
    lifecycle
  ]);
    console.log(result);
  } catch (e) {
    console.log(e);
  }
}

const lifecycle1 = {
  id: 'rule1',
  status: 'Enabled',
  prefix: 'foo/',
  expiration: {
    days: 3 // Specify that the current versions of objects expire three days after they are last modified.
  }
}
putBucketLifecycle(lifecycle1)

const lifecycle2 = {
  id: 'rule2',
  status: 'Enabled',
  prefix: 'foo/', 
  expiration: {
    createdBeforeDate: '2020-02-18T00:00:00.000Z' // Specify that the objects last modified before the specified date expire.
  },
}
putBucketLifecycle(lifecycle2)

const lifecycle3 = {
  id: 'rule3',
  status: 'Enabled',
  prefix: 'foo/', 
  abortMultipartUpload: {
    days: 3 // Specify the parts expire three days after they are last modified.
  },
}
putBucketLifecycle(lifecycle3)

const lifecycle4 = {
  id: 'rule4',
  status: 'Enabled',
  prefix: 'foo/', 
  abortMultipartUpload: {
    createdBeforeDate: '2020-02-18T00:00:00.000Z' // Specify that parts last modified before the specified date expire.
  },
}
putBucketLifecycle(lifecycle4)

const lifecycle5 = {
  id: 'rule5',
  status: 'Enabled',
  prefix: 'foo/', 
  transition: {
    // Specify that the storage class of the current versions of objects is converted to Archive 20 days after they are last modified.
    days: 20,
    storageClass: 'Archive'
  },
  expiration: {
    days: 21 // Specify that the current versions of objects expire 21 days after they are last modified.
  },
}
putBucketLifecycle(lifecycle5)

const lifecycle6 = {
  id: 'rule6',
  status: 'Enabled',
  prefix: 'foo/', 
  transition: {
    // Specify that the storage class of the objects that are last modified before the specified date is converted to Archive.
    createdBeforeDate: '2020-02-18T00:00:00.000Z', // Specify the date for conversion that is earlier than that for expiration.
    storageClass: 'Archive'
  },
  expiration: {
    createdBeforeDate: '2020-02-19T00:00:00.000Z' // Specify that the objects last modified before the specified date expire.
  },
}
putBucketLifecycle(lifecycle6)

const lifecycle7 = {
  id: 'rule7',
  status: 'Enabled',
  prefix: 'foo/', 
  noncurrentVersionExpiration: {
    noncurrentDays: 1 // Specify that objects expire one day after they become previous versions.
  },
}
putBucketLifecycle(lifecycle7)

const lifecycle8 = {
  id: 'rule8',
  status: 'Enabled',
  prefix: 'foo/', 
  expiredObjectDeleteMarker: true // Specify that delete markers are automatically removed when they expire.
}
putBucketLifecycle(lifecycle8)

const lifecycle9 = {
  id: 'rule9',
  status: 'Enabled',
  prefix: 'foo/', 
  // Specify that the storage class of previous versions of objects is converted to IA 10 days after they become previous versions.
  noncurrentVersionTransition: {
    noncurrentDays: '10',
    storageClass: 'IA'
  }
}
putBucketLifecycle(lifecycle9)

const lifecycle10 = {
  id: 'rule10',
  status: 'Enabled',
  prefix: 'foo/', 
  // Specify that the storage class of previous versions of objects is converted to IA 10 days after they become previous versions.
  noncurrentVersionTransition: {
    noncurrentDays: '10',
    storageClass: 'IA'
  },
  // Specify the tags for objects to match the rules.
  tag: [{
    key: 'key1',
    value: 'value1'
  },
   {
     key: 'key2',
     value: 'value2'
   }]
}
putBucketLifecycle(lifecycle10)

View lifecycle rules

The following code provides an example on how to use getBucketLifecycle to view the lifecycle rules configured for a bucket:

const OSS = require('ali-oss')
const client = new OSS({
  region: '<Your region>'
  accessKeyId: '<Your AccessKeyId>',
  accessKeySecret: '<Your AccessKeySecret>',
  bucket: '<Your bucket name>'
});
async function getBucketLifecycle () {
  try {
    let result = await client.getBucketLifecycle('<bucket-name>');
    console.log(result.rules); // Query the lifecycle rules.

    rules.forEach(rule => {
      console.log(rule.id) // View the rule IDs. 
      console.log(rule.status) // View the status of the rules.
      console.log(rule.tags) // View the tags configured in the lifecycle rules.
      console.log(rule.expiration.days) // View the validity period configurations.
      console.log(rule.expiration.createdBeforeDate) // View the expiration date configurations.
      // View the configurations used to delete expired parts.
      console.log(rule.abortMultipartUpload.days || rule.abortMultipartUpload.createdBeforeDate)
      // View the configurations used to convert storage classes.
      console.log(rule.transition.days || rule.transition.createdBeforeDate) // View the conversion date configurations.
      console.log(rule.transition.storageClass) // View the configurations used to convert storage classes.
      // View the configurations used to remove delete markers.
      console.log(rule.transition.expiredObjectDeleteMarker)
      // View the lifecycle rules to convert the storage class of the previous versions of the objects.
      console.log(rule.noncurrentVersionTransition.noncurrentDays) // View the conversion date configurations for previous versions of objects.
      console.log(rule.noncurrentVersionTransition.storageClass) // View the configurations used to convert the storage classes for previous versions.
      // View the expiration configurations for the previous versions of the objects.
      console.log(rule.noncurrentVersionExpiration.noncurrentDays)
    })
  } catch (e) {
    console.log(e);
  }
}
getBucketLifecycle();

Delete lifecycle rules

The following code provides an example on how to use deleteBucketLifecycle to delete the lifecycle rules configured for a bucket:

let OSS = require('ali-oss')
let client = new OSS({
  region: '<Your region>'
  accessKeyId: '<Your AccessKeyId>',
  accessKeySecret: '<Your AccessKeySecret>',
  bucket: '<Your bucket name>'
});
async function deleteBucketLifecycle () {
  try {
    let result = await client.deleteBucketLifecycle('bucket-name');
    console.log(result);
  } catch (e) {
    console.log(e);
  }
}
deleteBucketLifecycle();