Setting the expiration date of an S3 object (for deletion) using the JavaScript API

I am using the Node.js JavaScript API for Amazon AWS S3 and would like objects to expire a certain 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 after 100 days or so. Is it possible to set the expiration for deletion based on each object?

The documentation indicates that this is possible:

Amazon S3 provides an Expiration action that you can specify in your lifecycle configuration to expire objects.

...

When an item reaches the end of its life, Amazon S3 queues it for deletion and deletes it asynchronously. There may be a delay between the expiration date and the date on which Amazon S3 deletes the item. You do not pay for the storage time associated with the expiration of the object.

However, it seems to me that I should set this expiration in the bucket configuration, and not for each object when I load / create them.

The JavaScript SDK documentation indicates that I can set the Expires parameter when creating the object, but this is similar to 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 it is created?

 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); }); 
+5
source share
1 answer

You cannot set expiration rules for each object separately. To define expiration rules, you need to define the buck life cycle configuration .

To do this using the node.js API, see putBucketLifecycle call. You can also check the REST API documents for the PUT operation for the bucket life cycle .

+5
source

Source: https://habr.com/ru/post/1202353/


All Articles