Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ethagnawl/6ec8b53a49a7ae1bf2b44d0904f1c9e3 to your computer and use it in GitHub Desktop.
Save ethagnawl/6ec8b53a49a7ae1bf2b44d0904f1c9e3 to your computer and use it in GitHub Desktop.
copy s3 objects from one bucket to another
# 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