Automatically deleting objects older than n days i

2019-06-17 03:03发布

问题:

I am storing many images in Amazon S3, using a ruby lib (http://amazon.rubyforge.org/)

I don't care the photos older than 1 week, then to free the space in S3 I have to delete those photos.

I know there is a method to delete the object in a certain bucket:

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

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

If it does Not exist, I'll have to write a daemon to do that :-(

Thank you


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

回答1:

Unfortunately, Amazon doesn't offer an API for automatic deletion based on a specific set of criteria.

You'll need to write a daemon that goes through all of the photos and and selects just those that meet your criteria, and then delete them one by one.



回答2:

You can use the Amazon S3 Object Expiration policy

Amazon S3 - Object Expiration | AWS Blog

If you use S3 to store log files or other files that have a limited lifetime, you probably had to build some sort of mechanism in-house to track object ages and to initiate a bulk deletion process from time to time. Although our new Multi-Object deletion function will help you to make this process faster and easier, we want to go ever farther.

S3's new Object Expiration function allows you to define rules to schedule the removal of your objects after a pre-defined time period. The rules are specified in the Lifecycle Configuration policy that you apply to a bucket. You can update this policy through the S3 API or from the AWS Management Console.

Object Expiration | AWS S3 Documentation

Some objects that you store in an Amazon S3 bucket might have a well-defined lifetime. For example, you might be uploading periodic logs to your bucket, but you might need to retain those logs for a specific amount of time. You can use using the Object Lifecycle Management to specify a lifetime for objects in your bucket; when the lifetime of an object expires, Amazon S3 queues the objects for deletion.

Ps: Click on the links for more information.



回答3:

If you have access to a local database, it's easy to simply log each image (you may be doing this already depending on your application), and then you can perform a simple query to retrieve the entire list and delete them each. This is much faster than querying S3 directly, but does require local storage of some kind.