Object Storage Service (OSS) allows you to configure lifecycle rules to delete expired objects and parts or convert the storage class of expired objects to Infrequent Access (IA) or Archive to reduce your storage costs. This topic describes how to manage lifecycle rules for buckets.
Configure lifecycle rules for a bucket
The following code provides an example on how to configure lifecycle rules for 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',
// The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console.
accessKeyId: 'yourAccessKeyId',
accessKeySecret: 'yourAccessKeySecret',
// Set yourbucketname to the name of your 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: {
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 objects created before the specified date expire.
},
}
putBucketLifecycle(lifecycle2)
const lifecycle3 = {
id: 'rule3',
status: 'Enabled',
prefix: 'foo/',
abortMultipartUpload: {
days: 3 // Specify that 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 created 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 that the conversion date is earlier than the expiration date.
storageClass: 'Archive'
},
expiration: {
createdBeforeDate: '2020-02-19T00:00:00.000Z' // Specify that objects created 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 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 objects is converted to IA 10 days after they become previous versions.
noncurrentVersionTransition: {
noncurrentDays: '10',
storageClass: 'IA'
},
// Specify object tags that match the rules.
tag: [{
key: 'key1',
value: 'value1'
},
{
key: 'key2',
value: 'value2'
}]
}
putBucketLifecycle(lifecycle10)
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',
// The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console.
accessKeyId: 'yourAccessKeyId',
accessKeySecret: 'yourAccessKeySecret',
// Set Yourbucketname to the name of your 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 removed.
console.log(rule.transition.expiredObjectDeleteMarker)
// Query the rule to convert the storage classes of objects of previous versions.
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.
// Query the expiration configurations for previous versions of objects.
console.log(rule.noncurrentVersionExpiration.noncurrentDays)
})
} catch (e) {
console.log(e);
}
}
getBucketLifecycle();
Clear the lifecycle rules of a bucket
The following code provides an example on how to clear 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',
// The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console.
accessKeyId: 'yourAccessKeyId',
accessKeySecret: 'yourAccessKeySecret',
// Set Yourbucketname to the name of your 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 configure lifecycle rules for a bucket, visit GitHub.
- For more information about the API operation that you can call to configure lifecycle rules for a bucket, see PutBucketLifecycle.
- For more information about the API operation that you can call to query the lifecycle rules of a bucket, see GetBucketLifecycle.
- For more information about the API operation that you can call to clear the lifecycle rules of a bucket, see DeleteBucketLifecycle.