Created
January 27, 2017 03:58
-
-
Save ethagnawl/6ec8b53a49a7ae1bf2b44d0904f1c9e3 to your computer and use it in GitHub Desktop.
copy s3 objects from one bucket to another
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
# inspired by: https://gist.github.com/dje/763977 | |
def copy(source_bucket:, destination_bucket:, files:, connection:) | |
files.each_with_index { |file, index| | |
puts "copy #{file['Key']} to #{destination_bucket}" if index % 20 == 0 | |
connection.copy_object source_bucket, | |
file['Key'], | |
destination_bucket, | |
file['Key'] | |
} | |
end | |
connection = Fog::Storage.new({ | |
provider: 'AWS', | |
path_style: true, | |
aws_access_key_id: AWS_KEY, | |
aws_secret_access_key: AWS_SECRET | |
}) | |
source_bucket = 'old' | |
destination_bucket = 'new' | |
file_count = 0 | |
files = connection.get_bucket(source_bucket, prefix: 'nested-bucket') | |
file_count += files.body['Contents'].size | |
truncated = files.body['IsTruncated'] | |
the_response = files.body['Contents'] | |
copy( | |
files: files.body['Contents'], | |
source_bucket: source_bucket, | |
destination_bucket: destination_bucket, | |
connection: connection | |
) | |
while truncated | |
files = connection.get_bucket(source_bucket,{'max-keys' =>'100000', 'marker' => files.body['Contents'].last["Key"]}) | |
file_count += files.body['Contents'].size | |
truncated = files.body['IsTruncated'] | |
copy( | |
files: files.body['Contents'], | |
source_bucket: source_bucket, | |
destination_bucket: destination_bucket, | |
connection: connection | |
) | |
end | |
puts "******************** copied #{file_count} files" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment