Skip to content

Instantly share code, notes, and snippets.

@jsonperl
Created November 28, 2012 17:47
Show Gist options
  • Save jsonperl/4162825 to your computer and use it in GitHub Desktop.
Save jsonperl/4162825 to your computer and use it in GitHub Desktop.
Copy all s3 files from bucket to bucket
require 'aws/s3'
include AWS::S3
class S3Utils
include AWS::S3
def self.connect!
config = YAML.load(File.open("#{RAILS_ROOT}/config/amazon_s3.yml"))[RAILS_ENV]
Base.establish_connection!(
:access_key_id => config['access_key_id'],
:secret_access_key => config['secret_access_key'],
:server => "#{config['bucket']}.s3.amazonaws.com"
)
end
def self.copy_entire_bucket(from_bucket, to_bucket)
marker = '00000000'
result_count = 100
while result_count > 0 do
from_objects = Bucket.objects(from_bucket, :marker => marker, :max_keys => 100)
to_keys = Bucket.objects(to_bucket, :marker => marker, :max_keys => 100).collect {|o| o.key}
result_count = from_objects.length
from_objects.each do |o|
copy_between_buckets(o.key, from_bucket, to_bucket) if !to_keys.include?(o.key)
marker = o.key
end
end
end
def bucket_keys(bucket)
b = Bucket.find(bucket)
b.objects.collect {|o| o.key}
end
def self.copy_between_buckets(key, from_bucket, to_bucket)
S3Object.copy_to_bucket(key, from_bucket, to_bucket, :copy_acl => true)
end
def self.copy(from_key, to_key)
S3Object.copy(from_key, to_key, nil, :copy_acl => true)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment