Skip to content

Instantly share code, notes, and snippets.

@carlosgaldino
Last active September 27, 2016 02:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save carlosgaldino/8034527 to your computer and use it in GitHub Desktop.
Save carlosgaldino/8034527 to your computer and use it in GitHub Desktop.
Copy objects from multiple S3 buckets to another.
source 'https://rubygems.org'
gem 'aws-sdk', '~> 1.30.1'
GEM
remote: https://rubygems.org/
specs:
aws-sdk (1.30.1)
json (~> 1.4)
nokogiri (>= 1.4.4)
uuidtools (~> 2.1)
json (1.8.1)
mini_portile (0.5.2)
nokogiri (1.6.1)
mini_portile (~> 0.5.0)
uuidtools (2.1.4)
PLATFORMS
ruby
DEPENDENCIES
aws-sdk (~> 1.30.1)
#!/usr/bin/env ruby
#
# Copy objects from multiple S3 buckets to another.
#
# Example:
#
# move-s3-objects target-bucket first-bucket second-bucket
#
# The required gems are specified on the Gemfile.
#
# Requires your aws credentials in ~/.aws-credentials.yml file with the following
# keys:
#
# access_key_id
# secret_access_key
# region
require 'rubygems'
require 'bundler/setup'
require 'aws-sdk'
credentials_path = File.expand_path('~/.aws-credentials.yml')
unless File.exist?(credentials_path)
puts <<-STR
You need to add your credentials in '~/.aws-credentials.yml' with the following information:
access_key_id
secret_access_key
region
STR
exit!(1)
end
credentials = YAML::load_file(credentials_path)
s3 = AWS::S3.new(access_key_id: credentials['access_key_id'],
secret_access_key: credentials['secret_access_key'],
region: credentials['region'])
new_bucket_name, *source_buckets_names = ARGV
source_buckets = s3.buckets.select { |b| source_buckets_names.include?(b.name) }
new_bucket = s3.buckets.create(new_bucket_name)
collections = source_buckets.map(&:objects)
collections.each do |collection|
collection.each do |object|
begin
object.copy_to(object.key, bucket: new_bucket, acl: :public_read)
puts " OK: #{object.key}"
rescue Exception => e
puts " FAIL: #{object.key}"
puts '*' * 30
puts e.message
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment