Automatically delete objects older than n days in Amazon S3 (How?)

I store many images in Amazon S3 using ruby ​​lib ( http://amazon.rubyforge.org/ )

I don’t like photos older than 1 week, then to free up space in S3 I need to delete these photos.

I know that there is a method for deleting an object in a specific bucket:

S3Object.delete 'photo-1.jpg', 'photos' 

Is there a way to automatically delete an image older than a week?

If this does not exist, I will have to write a daemon to do this: - (

thanks


UPDATE: it is now possible, check Roberto's answer.

+7
ruby ruby-on-rails amazon-s3 amazon-web-services
source share
3 answers

Unfortunately, Amazon does not offer an API for automatic deletion based on a specific set of criteria.

You will need to write a demon that will go through all the photos, and select only those that match your criteria, and then delete them one by one.

+1
source share

You can use the Amazon S3 Resource Expiration Policy

Amazon S3 - Expiration | AWS Blog

If you use S3 to store log files or other files that are limited, you probably should have built some mechanism inside to track the age objects of the object and initiate the process of deleting large numbers from time to time. Although our new multi-object delete function will help you make this process faster and easier, we want to go further.

The new S3 expiration function allows you to define rules for plan the removal of objects after a specified period of time. The rules are specified in the lifecycle configuration policy that you apply to the bucket. You can update this policy through the S3 API or from the AWS management console.

Expiration | AWS S3 Documentation

Some items that you store in your Amazon S3 bucket may have a clearly defined life span. For example, you can load periodic logs in your bucket, but you may need to save these logs for a certain amount of time. You can use it through the life cycle of the Management object to specify the lifetime for the objects in your bucket; when an item expires, Amazon S3 queues the items for deletion.

Ps: Click the link for more information.

+42
source share

If you have access to a local database, it is easy to simply register each image (you can do this already depending on your application), and then you can perform a simple query to get the entire list and delete them each. This is much faster than an S3 request directly, but it requires local storage.

+1
source share

All Articles