Skip to content

Instantly share code, notes, and snippets.

@jrust
Created August 30, 2012 18:10
Show Gist options
  • Save jrust/3536024 to your computer and use it in GitHub Desktop.
Save jrust/3536024 to your computer and use it in GitHub Desktop.
Copy one S3 bucket to another
require 'rubygems'
require 'aws-sdk'
aws_access_key_id = 'key-id'
aws_secret_access_key = 'access-key'
s3 = AWS::S3.new :access_key_id => aws_access_key_id, :secret_access_key => aws_secret_access_key
target_bucket = s3.buckets['old-bucket']
destination_bucket = s3.buckets['new-bucket']
# Set a prefix to copy just a portion of the bucket.
# Useful for limiting copy or spinning off several processes copying different parts of the bucket simultaneously
prefix = ''
# Set cache control if desired
cache_control = 'max-age=29030400'
# Set ACL
acl = :public_read
copied_keys = destination_bucket.objects.with_prefix(prefix).map(&:key)
target_bucket.objects.with_prefix(prefix).each do |obj|
if copied_keys.include?(obj.key)
puts "#{obj.key} already exists, skipping."
else
obj.copy_to obj.key, bucket: destination_bucket, acl: acl, :cache_control => cache_control
puts "#{obj.key} copied to new bucket."
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment