Skip to content

Instantly share code, notes, and snippets.

@sidraval
Last active August 29, 2015 13:57
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 sidraval/9716542 to your computer and use it in GitHub Desktop.
Save sidraval/9716542 to your computer and use it in GitHub Desktop.
Copy one S3 bucket's contents to another bucket; supports file > 5gb. Also copies over Access Control List options and encryption options. Won't overwrite files that already exist on the bucket you're copying to.
require 'aws-sdk'
class S3Copy
attr_reader :from_bucket, :to_bucket
def initialize(from_bucket, to_bucket)
access_key = ENV['AWS_ACCESS_KEY_ID']
secret_key = ENV['AWS_SECRET_ACCESS_KEY']
s3_instance = AWS::S3.new(access_key_id: access_key, secret_access_key: secret_key)
@from_bucket = s3_instance.buckets[from_bucket]
@to_bucket = s3_instance.buckets[to_bucket]
end
def perform_copy
from_bucket.objects.each do |f_obj|
t_obj = to_bucket.objects[f_obj.key]
options = {}
next if t_obj.exists?
options.merge!({ use_multipart_copy: true }) if f_obj.content_length > 5368709120
options.merge!({ server_side_encryption: f_obj.server_side_encryption }) if f_obj.server_side_encryption?
t_obj.copy_from(f_obj, options)
t_obj.acl = f_obj.acl
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment