Last active
December 12, 2024 23:16
-
-
Save cschulte22/742d5083e4d9a8866443 to your computer and use it in GitHub Desktop.
Ruby script to copy files from one S3 bucket to another using aws-sdk
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
require 'aws-sdk' | |
class Transfer | |
def initialize(from_bucket, to_bucket) | |
@s3 = AWS::S3.new | |
@from_bucket = @s3.buckets[from_bucket] | |
@to_bucket = @s3.buckets[to_bucket] | |
raise "#{from_bucket} does not exist" unless @from_bucket.exists? | |
raise "#{to_bucket} does not exist" unless @to_bucket.exists? | |
end | |
def transfer(for_real = false) | |
puts "Loading keys..." | |
keys = load_keys_to_transfer(@from_bucket) | |
puts " - Found #{keys.size} keys to transfer" | |
stats = Hash.new(0) | |
count = 0 | |
for key in keys do | |
count += 1 | |
print "Copying %5d/%5d - " % [count, keys.size] | |
pkey = key.clone | |
if key.length > 50 | |
pkey = "...#{key[-50,50]}" | |
end | |
puts pkey | |
from_object = @from_bucket.objects[key] | |
to_object = @to_bucket.objects[key] | |
if to_object.exists? | |
stats[:already_copied] += 1 | |
else | |
begin | |
from_object.copy_to(to_object, :acl => :private) if for_real | |
stats[:copied] += 1 | |
rescue Exception => ex | |
stats[:errors] += 1 | |
puts " --> EXCEPTION: #{ex.message} (key: #{key})" | |
end | |
end | |
end | |
puts "All done with #{keys.size} keys" | |
puts "Stats: #{stats.inspect}" | |
end | |
private | |
def load_keys_to_transfer(bucket) | |
keys = [] | |
bucket.objects.each { |obj| keys << obj.key } | |
return keys | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment