Skip to content

Instantly share code, notes, and snippets.

@vparihar01
Created June 28, 2014 12:33
Show Gist options
  • Save vparihar01/ecfac730aad65ca62c0f to your computer and use it in GitHub Desktop.
Save vparihar01/ecfac730aad65ca62c0f to your computer and use it in GitHub Desktop.
Copying files between S3 accounts.
#!/usr/bin/ruby
require 'rubygems'
require 'right_aws'
# ACL property differences
old_owner_id='xxxxxxx'
new_owner_id='xxxxx'
oldAWS = RightAws::S3Interface.new('access_key_old','secret_key_old')
newAWS = RightAws::S3Interface.new('access_key_new','secret_key_new')
newS3=RightAws::S3.new('access_key_new','secret_key_old')
bucket_mapping={"stage" => "demo"
}
old_disp_name='my.old.name'
new_disp_name='my.new.name'
bucket_mapping.each do |old_bucket, new_bucket|
# get all keys for old bucket by looping through sets of max keys (1000) amazon gives
newS3Bucket=newS3.bucket(new_bucket)
oldAWS.incrementally_list_bucket(old_bucket) do |key_set|
# loop through content of key_set which contains keys
key_set[:contents].each do |key|
# if key already exists, dont copy over
if newS3Bucket.key(key[:key]).exists?
puts "#{new_bucket} #{key[:key]} already exists. Skipping..."
else
# download data and header from old bucket
puts "Copying #{old_bucket} #{key[:key]}"
retries=0
begin
data=oldAWS.get_object(old_bucket,key[:key])
rescue Exception => e
puts "cannot download, #{e.inspect}nretrying #{retries} out of 10 times..."
retries += 1
retry if retries e
puts "cannot get header, #{e.inspect}nretrying #{retries} out of 10 times..."
retries += 1
retry if retries e
puts "cannot put object, #{e.inspect}nretrying #{retries} out of 10 times..."
retries += 1
retry if retries e
puts "cannot get ACL, #{e.inspect}nretrying #{retries} out of 10 times..."
retries += 1
retry if retries e
puts "cannot update ACL, #{e.inspect}nretrying #{retries} out of 10 times..."
retries += 1
retry if retries <= 10
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment