Lifecycle rules using OSS SDK for Node.js

更新时间:
复制 MD 格式

Lifecycle rules let you automatically transition objects to colder storage classes or delete them based on the last modification time, reducing 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.

  • Configuring lifecycle rules requires the oss:PutBucketLifecycle permission, querying requires oss:GetBucketLifecycle, and deleting requires oss:DeleteBucketLifecycle. For more information, see Grant a custom policy.

Configure lifecycle rules for a bucket

The following sample code configures lifecycle rules based on the last modification time for a bucket. To modify existing rules, follow 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 lifecycle rules

The following sample code queries 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. 

    result.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 lifecycle rules

The following sample code deletes all lifecycle rules of a bucket. To delete specific rules, follow 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

  • The complete sample code for lifecycle rules is on 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.