14
votes

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);
});
1

1 Answers

16
votes

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.