Last active
September 27, 2016 02:18
-
-
Save carlosgaldino/8034527 to your computer and use it in GitHub Desktop.
Copy objects from multiple S3 buckets to another.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'https://rubygems.org' | |
gem 'aws-sdk', '~> 1.30.1' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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