Skip to content

Instantly share code, notes, and snippets.

@camelpunch
Created September 22, 2010 11:30
Show Gist options
  • Save camelpunch/591524 to your computer and use it in GitHub Desktop.
Save camelpunch/591524 to your computer and use it in GitHub Desktop.
ruby_block "copy S3 objects from production to staging" do
block do
require 'rubygems'
require 'right_aws'
aws_access_key_id = credentials['amazon_access_key_id']
aws_secret_access_key = credentials['amazon_secret_access_key']
target_bucket = 'source'
destination_bucket = 'destination'
s3 = RightAws::S3Interface.new(aws_access_key_id, aws_secret_access_key)
copied_keys = Array.new
s3.incrementally_list_bucket(destination_bucket) do |key_set|
copied_keys << key_set[:contents].map{|k| k[:key]}.flatten
end
copied_keys.flatten!
s3.incrementally_list_bucket(target_bucket) do |key_set|
key_set[:contents].each do |key|
key = key[:key]
if copied_keys.include?(key)
Chef::Log.info "#{destination_bucket} #{key} already exists. Skipping..."
else
Chef::Log.info "Copying #{target_bucket} #{key}"
retries = 0
begin
s3.copy(target_bucket, key, destination_bucket)
rescue Exception => e
Chef::Log.error "cannot copy key, #{e.inspect}\nretrying #{retries} out of 10 times..."
retries += 1
retry if retries <= 10
end
Chef::Log.info "Copying ACL for #{target_bucket} #{key}"
retries = 0
begin
acl = s3.get_acl(target_bucket, key)
s3.put_acl(destination_bucket, key, acl[:object])
rescue Exception => e
Chef::Log.error "cannot copy ACL, #{e.inspect}\nretrying #{retries} out of 10 times..."
retries += 1
retry if retries <= 10
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment