Skip to content

Instantly share code, notes, and snippets.

@amasses
Created March 22, 2010 04:23
Show Gist options
  • Save amasses/339795 to your computer and use it in GitHub Desktop.
Save amasses/339795 to your computer and use it in GitHub Desktop.
development:
access_key_id: YOUR-KEY-ID
secret_access_key: YOUR-SECRET-KEY
test:
access_key_id:
secret_access_key:
production:
access_key_id: YOUR-KEY-ID
secret_access_key: YOUR-SECRET-KEY
require 'aws/s3'
CONFIG = YAML.load_file("config/amazon_s3.yml")
CONFIG.symbolize_keys!
CONFIG.each_pair{|x,y| y.symbolize_keys! }
AWS::S3::Base.establish_connection! CONFIG[:production]
AWS::S3::S3Object.class_eval do
def copy_to_bucket(copy_name, target_bucket, options = {})
self.class.copy_to_bucket(key, copy_name, bucket.name, target_bucket, options)
end
def self.copy_to_bucket(key, copy_key, bucket = nil, to_bucket = nil, options = {})
bucket = bucket_name(bucket)
source_key = path!(bucket, key)
default_options = {'x-amz-copy-source' => source_key, 'x-amz-metadata-directive' => "COPY"}
target_key = path!(to_bucket || bucket, copy_key)
returning put(target_key, default_options) do
acl(copy_key, to_bucket || bucket, acl(key, bucket)) if options[:copy_acl]
end
end
end
source_bucket = "bucket-name"
target_bucket = "target-bucket-name"
AWS::S3::Bucket.objects(source_bucket).each do |x|
if AWS::S3::S3Object.exists? x.key, source_bucket
puts "Skipping #{x.key}, file exists in #{target_bucket}"
next
end
x.copy_to_bucket(x.key, target_bucket)
puts "Copied #{x.key} to #{target_bucket}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment