How to set an expiration rule for an S3 object (not bucket)?


I have an S3 bucket containing a number of objects that fall into three classes. They fall into three classes:

  1. objects that should expire in 15 minutes,
  2. objects that should expire after a day and
  3. objects that should never expire automatically.

I would like to use native AWS functionality to do this, rather than running a separate process to scan the bucket and delete objects (yuck).

I really want to avoid having three buckets, with a bucket-level lifecycle rule on each with different expiration periods as it's far easier in my application to set the expiration at object creation time, if this is actually possible.

I think I can do this with three tags and a lifecycle configuration on the bucket, but this seems to have a granularity of 1 day, i.e. I can't delete in less than a day. Setting the expiry date per object would fix this but I can't see any way to do this.

Any ideas welcome :)


asked 9 months ago2159 views
2 Answers
Accepted Answer

Unfortunately, S3 lifecycle policy only supports expiration by days, so if you want to expire your objects in minutes, you need to develop your own solution.

As a possible solution, after you upload an S3 object that you want to expire in 15 minutes, you push its bucket and key info into an SQS queue. You can delay the message visibility for 15 minutes so that your queue processing function can "delay" the deletion for 15 minutes after the object was upload.

profile picture
answered 9 months ago
profile picture
reviewed 9 months ago

Nice - I like the queue solution!

answered 9 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions