Les Hill github twitter facebook linked in archives
Posted May 28, 2009

We use S3 for a lot of our projects using the aws-s3 gem. One thing that has continually plagued me is copying objects from one bucket to another. Having rewritten this snippet of code twice already, I am placing it here for future reference. Enjoy.

 1 class S3Object
 2   def self.copy_across_buckets(src_bucket, src_key, dest_bucket, dest_key, options = {})
 3     original = open(url_for(src_key, src_bucket))
 4     default_options = {:content_type => original.content_type}
 5     store(dest_key, original, dest_bucket, default_options.merge(options))
 6     acl(dest_key, dest_bucket, acl(src_key, src_bucket))
 7   end
 8 
 9   def copy_to_bucket(dest_bucket, dest_key = nil, options = {})
10     self.class.copy_across_buckets(bucket.name, key, dest_bucket, dest_key ? dest_key : key, options)
11   end
12 end

Thanks to Tom Preston-Werner for the CSS layout, Webby for the blog renderer, and GitHub Pages for the blog hosting.