Not all data uploaded to OSS requires frequent access. For purposes such as data compliance or archiving, some data must be stored in a cold storage class. In other scenarios, you may want to batch delete data that is no longer needed from a bucket. You can configure lifecycle rules based on the Last Modified Time to reduce storage costs. These rules can periodically transition objects from a hot storage class to a cold storage class, or delete the objects.
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 set lifecycle rules, you must have the
oss:PutBucketLifecyclepermission. To view lifecycle rules, you must have theoss:GetBucketLifecyclepermission. To delete all lifecycle rules, you must have theoss:DeleteBucketLifecyclepermission. For more information, see Grant custom access policies to RAM users.
Set lifecycle rules
The following code shows how to set lifecycle rules based on the Last Modified Time. After the rules are set, if you want to modify one or more of them, see How do I modify one or more lifecycle rule configurations?.
const OSS = require('ali-oss')
const client = new OSS({
// Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set 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 set.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the bucket name.
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: {
// Expire the current version of the object 3 days after its last modified time.
days: 3
}
}
putBucketLifecycle(lifecycle1)
const lifecycle2 = {
id: 'rule2',
status: 'Enabled',
prefix: 'foo/',
expiration: {
// Expire objects created before the specified date.
createdBeforeDate: '2020-02-18T00:00:00.000Z'
},
}
putBucketLifecycle(lifecycle2)
const lifecycle3 = {
id: 'rule3',
status: 'Enabled',
prefix: 'foo/',
abortMultipartUpload: {
// Expire multipart upload parts after 3 days.
days: 3
},
}
putBucketLifecycle(lifecycle3)
const lifecycle4 = {
id: 'rule4',
status: 'Enabled',
prefix: 'foo/',
abortMultipartUpload: {
// Expire multipart upload parts created before the specified date.
createdBeforeDate: '2020-02-18T00:00:00.000Z'
},
}
putBucketLifecycle(lifecycle4)
const lifecycle5 = {
id: 'rule5',
status: 'Enabled',
prefix: 'foo/',
transition: {
// Transition the current version of the object to the Archive Storage class 20 days after its last modified time.
days: 20,
storageClass: 'Archive'
},
expiration: {
// Expire the current version of the object 21 days after its last modified time.
days: 21
},
}
putBucketLifecycle(lifecycle5)
const lifecycle6 = {
id: 'rule6',
status: 'Enabled',
prefix: 'foo/',
transition: {
// Transition objects created before the specified date to the Archive Storage class.
createdBeforeDate: '2023-02-19T00:00:00.000Z',
storageClass: 'Archive'
},
expiration: {
// Delete objects created before the specified date.
createdBeforeDate: '2023-01-18T00:00:00.000Z'
},
}
putBucketLifecycle(lifecycle6)
const lifecycle7 = {
id: 'rule7',
status: 'Enabled',
prefix: 'foo/',
expiration: {
// Automatically remove expired delete markers.
expiredObjectDeleteMarker: true
}
}
putBucketLifecycle(lifecycle7)
const lifecycle8 = {
id: 'rule8',
status: 'Enabled',
prefix: 'foo/',
// Transition non-current versions of objects to the Infrequent Access (IA) storage class 10 days after they become non-current.
noncurrentVersionTransition: {
noncurrentDays: '10',
storageClass: 'IA'
}
}
putBucketLifecycle(lifecycle8)
const lifecycle9 = {
id: 'rule9',
status: 'Enabled',
prefix: 'foo/',
// Transition non-current versions of objects to the Infrequent Access (IA) storage class 10 days after they become non-current.
noncurrentVersionTransition: {
noncurrentDays: '10',
storageClass: 'IA'
},
// Specify the object tags to which the rule applies.
tag: [{
key: 'key1',
value: 'value1'
},
{
key: 'key2',
value: 'value2'
}]
}
putBucketLifecycle(lifecycle9)View lifecycle rules
The following code shows how to view the lifecycle rules.
const OSS = require('ali-oss')
const client = new OSS({
// Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set 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 set.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the bucket name.
bucket: 'yourbucketname'
});
async function getBucketLifecycle () {
try {
const result = await client.getBucketLifecycle('Yourbucketname');
console.log(result.rules); // Get the lifecycle rules.
result.rules.forEach(rule => {
console.log(rule.id) // View the lifecycle rule ID.
console.log(rule.status) // View the status of the lifecycle rule.
console.log(rule.tags) // View the tags of the lifecycle rule.
console.log(rule.expiration.days) // View the expiration rule based on the number of days.
console.log(rule.expiration.createdBeforeDate) // View the expiration rule based on the date.
// View the rule for expiring multipart upload parts.
console.log(rule.abortMultipartUpload.days || rule.abortMultipartUpload.createdBeforeDate)
// View the storage class transition rule.
console.log(rule.transition.days || rule.transition.createdBeforeDate) // View the time for storage class transition.
console.log(rule.transition.storageClass) // The storage class to which objects are transitioned.
// View whether to automatically delete expired delete markers.
console.log(rule.transition.expiredObjectDeleteMarker)
// View the storage class transition rule for non-current object versions.
console.log(rule.noncurrentVersionTransition.noncurrentDays) // View the time for storage class transition for non-current object versions.
console.log(rule.noncurrentVersionTransition.storageClass) // View the storage class to which non-current object versions are transitioned.
})
} catch (e) {
console.log(e);
}
}
getBucketLifecycle();Delete lifecycle rules
The following code shows how to delete all lifecycle rules from a bucket. To delete only one or more specific lifecycle rules, see How do I delete one or more lifecycle rules?.
const OSS = require('ali-oss')
const client = new OSS({
// Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set 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 set.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the bucket name.
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 for lifecycle rules, see GitHub examples.
For more information about the API operation used to set lifecycle rules, see PutBucketLifecycle.
For more information about the API operation used to view lifecycle rules, see GetBucketLifecycle.
For more information about the API operation used to delete all lifecycle rules, see DeleteBucketLifecycle.