How to exclude an prefix when setting AWS S3 Lifecycle policy?

Situatation:

For example, you have 1 bucket that have 9 folders (named folder1 -> folder9):

  • You want to expire data in folder 1 -> 8 in 1 month

  • You want to expire data in folder 9 in 4 month

\=> Because AWS S3 do not have folder definition, only prefix, you have to set up policy for each prefix (~ 9 policy for 9 prefix). That’s not good, you want to exclude only 1 folder named folder9. How can you?

Solution:

Solution 1:

The easiest way is that you re-arrange the structure of you S3 bucket (for e.g, you collect folder1 -> folder8 into 1 prefix, and folder9 in another prefix). However, sometimes you can not use this solution because the business of your infrastructure do not allow!

Solution 2:

You should define tags for your objects and filter objects included in the S3 lifecycle based on object tags.

To know more about this, please visit the document [1].

Note the following:
– You have to create tags for your existing objects. (You can do this automatically by using S3 Batch job (Refer to the document [2])
– Each tag must match both key and value exactly.
– The rule applies to a subset of objects that has all the tags specified in the rule. If an object has additional tags specified, the rule still applies.

References:
[1] Simplify your data lifecycle by using object tags with Amazon S3 Lifecycle
 https://aws.amazon.com/blogs/storage/simplify-your-data-lifecycle-by-using-object-tags-with-amazon-s3-lifecycle/

[2] Adding and removing object tags with Amazon S3 Batch Operations

https://aws.amazon.com/blogs/storage/adding-and-removing-object-tags-with-s3-batch-operations/