Greetings :)

Hi, I'm Louis!
I like to code, but I don't do enough of it.

This blog is about trying my best to keep up with the ever evolving stream of technology.

Friday, August 5, 2011

Deleting objects with ruby on Amazon S3

Problem:
I ended up with loads of objects in my bucket after configuring some logging in Amazon S3. Deleting these manually is not the best use of time (click click click... one has been deleted) * 1 million (slightly over-exaggerated)

Solution:
Use Ruby to do it!

Solution Problem:
I was trying to use the 'aws/s3' gem, and apparently there's some problems with european regions or something... anyway, I tried some sample code found in http://stackoverflow.com/questions/27267/delete-amazon-s3-buckets, and then I found - http://thewebfellas.com/blog/2009/8/29/protecting-your-paperclip-downloads which led me to my eventual solution...

Solution Problem Solution:
Use a different gem - the 's3' gem.

Here's what I ended up with:

require 's3'

service = S3::service.new(:access_key_id => your key from amazon account page, 
                          :secret_access_key => your secret key from amazon account page)

my_bucket = service.buckets.find("my-unique-bucket-name")
my_bucket.objects.each {|object| object.destroy}

Pretty simple in the end huh :)

Check that it worked by doing my_bucket.objects.size before and after you delete the objects.

1 comment: