I am using the Node.js JavaScript API for Amazon AWS S3, and would like to set objects to expire a specified number of days after the objects are created. That is, if I create and upload a new object, I want it to automatically delete itself 100 days or so from now. Is this possible to set expiration for deletion on a per-object basis?
The documentation indicates this may be possible:
Amazon S3 provides an Expiration action that you can specify in your lifecycle configuration to expire objects.
…
When an object reaches the end of its lifetime, Amazon S3 queues it for removal and removes it asynchronously. There may be a lag between the expiration date and the date at which Amazon S3 removes an object. You are not charged for storage time associated with an object that has expired.
However, it seems that I would have to set this expiration in the bucket configuration, and not per-object when I upload/create them.
The JavaScript SDK documentation indicates that I can set an Expires
parameter when creating an object, but this seems to be for the Expires
HTTP header when S3 returns the object for subsequent GET requests.
Is there a way to set the expiration date of an object when creating it?
s3.putObject({
Bucket: config.s3.bucketName,
Key: s3Key,
Body: objBuffer,
ACL: 'public-read',
ContentType: 'image/jpeg',
StorageClass: 'REDUCED_REDUNDANCY',
// Some option here for setting expiration/deletion date?
}, function () {
console.log(arguments);
});
Amazon S3 transitions the current version of the object to the specified storage class. Same behavior as a versioning-enabled bucket. When a specified date or time period in the object's lifetime is reached. Expiration deletes the object, and the deleted object cannot be recovered.
If the versioning is disabled, you can run the aws s3 rm CLI command to delete all objects in the S3 bucket. If versioning is enabled, you run the CLI command aws s3api delete-objects to delete all versioned objects in the S3 bucket. Once the S3 bucket is empty you can then proceed to delete it.
During this time, based on their expiration dates, any object found to be expired will be queued for removal. You will not be billed for any associated storage for those objects on or after their expiration date. If server access logging has been enabled for that S3 bucket, an S3. EXPIRE.
You can not set expiration rules on each object individually. To define object expiration rules, you have to define a bucket lifecycle configuration.
To do this with the node.js API, see the putBucketLifecycle call. You can also check out the REST API docs for the bucket lifecycle PUT operation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With