Created
November 10, 2013 14:24
-
-
Save unnitallman/7398887 to your computer and use it in GitHub Desktop.
Moving S3 objects from one bucket to another, along with URL obfuscation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
secret = "a_big_secret" | |
#expects S3 credentials in congif/aws.yml | |
s3 = AWS::S3.new | |
i = 0 | |
source = 'source_bucket_name' | |
target = 'target_bucket_name' | |
all_objects = s3.buckets[source].objects | |
migrated_keys = open("migrated_objects.txt").read.split("\n").map{|x| x.split(',').first} | |
remaining_objects = all_objects.reject{|obj| migrated_keys.include?(obj.key)} | |
puts "Total objects = #{all_objects.count}" | |
puts "Already migrated = #{migrated_keys.count}" | |
puts "Remaining = #{remaining_objects.count}" | |
remaining_objects.each do |obj| | |
old_key = obj.key | |
puts "#{i=i+1}/#{remaining_objects.count}" | |
filename = old_key.split('/').last | |
hash = OpenSSL::HMAC.hexdigest OpenSSL::Digest::SHA1.new, secret, old_key | |
new_key = "#{hash}/#{old_key}" | |
obj.copy_to new_key, {:acl => :public_read, :bucket_name => target} | |
open("migrated_objects.txt", "a") do |f| | |
k = "#{old_key},#{new_key}" | |
f.puts k | |
puts k | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment