All Products
Search
Document Center

Object Storage Service:Use OSS SDK for Node.js to manage lifecycle rules

Last Updated:Nov 05, 2024

Not all data that is uploaded to Object Storage Service (OSS) buckets is frequently accessed. Rarely accessed data is stored in cold storage for compliance and archiving purposes. You may want to delete data that no longer needs to be stored in batches from your buckets based on your business scenario. In this case, you can configure lifecycle rules based on the last modification time of objects to periodically change the storage class of objects from hot to cold or delete objects to reduce storage costs.

Usage notes

  • Before you configure lifecycle rules based on the last modified time of objects, make sure that you familiarize yourself with this feature. For more information, see Lifecycle rules based on the last modified time.

  • To configure lifecycle rules, you must have the oss:PutBucketLifecycle permission. To query lifecycle rules, you must have the oss:GetBucketLifecycle permission. To delete lifecycle rules, you must have the oss:DeleteBucketLifecycle permission. For more information, see Attach a custom policy to a RAM user.

Configure lifecycle rules for a bucket

The following code provides an example on how to configure lifecycle rules based on the last modification time of objects for a bucket. After you configure lifecycle rules, you can modify the lifecycle rules based on your business requirements. For information about how to modify existing lifecycle rules, see Lifecycle rules based on the last modified time.

const OSS = require('ali-oss')

const client = new OSS({
  // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
  region: 'yourregion',
  // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Specify the name of the bucket. 
  bucket: 'yourbucketname'
});

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

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

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

const lifecycle3 = {
  id: 'rule3',
  status: 'Enabled',
  prefix: 'foo/', 
  abortMultipartUpload: {
    // Specify that parts expire in three days. 
    days: 3 
  },
}
putBucketLifecycle(lifecycle3)

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

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

const lifecycle6 = {
  id: 'rule6',
  status: 'Enabled',
  prefix: 'foo/', 
  transition: {
    //Specify that the storage classes of the objects that are created before the specified date are changed to Archive. 
    createdBeforeDate: '2023-02-19T00:00:00.000Z', 
    storageClass: 'Archive'
  },
  expiration: {
    // Specify that objects created before the specified date are deleted. 
    createdBeforeDate: '2023-01-18T00:00:00.000Z' 
  },
}
putBucketLifecycle(lifecycle6)

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

const lifecycle8 = {
  id: 'rule8',
  status: 'Enabled',
  prefix: 'foo/', 
  // Specify that the storage classes of the previous versions of objects are changed to IA 10 days after the objects are last modified. 
  noncurrentVersionTransition: {
    noncurrentDays: '10',
    storageClass: 'IA'
  }
}
putBucketLifecycle(lifecycle8)

const lifecycle9 = {
  id: 'rule9',
  status: 'Enabled',
  prefix: 'foo/', 
  // Specify that the storage classes of the previous versions of objects are changed to IA 10 days after the objects are last modified. 
  noncurrentVersionTransition: {
    noncurrentDays: '10',
    storageClass: 'IA'
  },
  // Specify the tags for objects that you want to match the rules. 
  tag: [{
    key: 'key1',
    value: 'value1'
  },
   {
     key: 'key2',
     value: 'value2'
   }]
}
putBucketLifecycle(lifecycle9)

Query the lifecycle rules of a bucket

The following code provides an example on how to query the lifecycle rules of a bucket:

const OSS = require('ali-oss')

const client = new OSS({
  // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
  region: 'yourregion',
  // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Specify the name of the bucket. 
  bucket: 'yourbucketname'
});

async function getBucketLifecycle () {
  try {
    const result = await client.getBucketLifecycle('Yourbucketname');
    console.log(result.rules); // Query the lifecycle rules. 

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

Delete the lifecycle rules of a bucket

The following code provides an example on how to delete all lifecycle rules of a bucket. If you want to delete one or more lifecycle rules, refer to Lifecycle rules based on the last modified time.

const OSS = require('ali-oss')

const client = new OSS({
  // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
  region: 'yourregion',
  // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Specify the name of the bucket. 
  bucket: 'yourbucketname'
});

async function deleteBucketLifecycle () {
  try {
    const result = await client.deleteBucketLifecycle('Yourbucketname');
    console.log(result);
  } catch (e) {
    console.log(e);
  }
}
deleteBucketLifecycle();

References

  • For the complete sample code that is used to manage lifecycle rules, visit GitHub.

  • For information about the API operation that you can call to configure a lifecycle rule, see PutBucketLifecycle.

  • For information about the API operation that you can call to query lifecycle rules, see GetBucketLifecycle.

  • For information about the API operation that you can call to delete lifecycle rules, see DeleteBucketLifecycle.