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
 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

Glad you liked it. Would you like to share?

Sharing this page …

Thanks! Close

Add New Comment

Showing 1 comment

Sort by   Subscribe by email   Subscribe by RSS
  • You are downloading the object to your filesystem, then re-uploading to a new bucket.

    A faster and more effective way to do this, is to use Amazon native copy command, so you can copy from one bucket to another on the fly without downloading it.

    here is a link: http://www.jamesmurty.com/2008...
Trackback URL 
blog comments powered by Disqus

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