s3-migrator 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 662bedcc18bb924505996a41af1b1de5d338e6c1
4
+ data.tar.gz: df303f0a98ed9498a152ab9f9dbb78422efb4cf4
5
+ SHA512:
6
+ metadata.gz: 0c95a738fe5c56a4014446c55815942f9125de64b34a99943f25cf6ff06a402136bc5ab255eb88d284a516b20218d7312f26dfbb027ff3f3f4d2cbe728aa8e38
7
+ data.tar.gz: 5bdc61a2497fbc581107a2ba2e6a4c22726dd7b72f32ca43629a50c73cdfbd23bcd0dd9b277df94c9bad96f9774a6d41b9056a836818aade163b16334213cac6
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in s3-migrator.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 yoppi
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # S3::Migrator
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 's3-migrator'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install s3-migrator
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/s3-migrator/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/s3-migrator ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative "../lib/s3/migrator"
3
+
4
+ opts = {}
5
+ OptionParser.new { |o|
6
+ o.banner = "Usage: s3-migrator.rb [options]"
7
+ o.on("-i", "--access-key-id, AWS_ACCESS_KEY_ID") { |v| opts[:access_key_id] = v }
8
+ o.on("-k", "--secret-access-key AWS_SECRET_ACCESS_KEY") { |v| opts[:secret_access_key] = v }
9
+ o.on("-r", "--region REGEION") { |v| opts[:region] = v }
10
+ o.on("-s", "--src-bucket SOURCE_BUCKET") { |v| opts[:src_bucket] = v }
11
+ o.on("-d", "--dest-bucket DESTINATION_BUCKET", ) { |v| opts[:dest_bucket] = v }
12
+ o.on("-p", "--prefix [PREFIX]") { |v| opts[:prefix] = v }
13
+ o.on("-t", "--thread [COUNT]") { |v| opts[:thread_count] = v }
14
+ }.parse!(ARGV)
15
+
16
+ S3::Migrator.migrate!(opts)
@@ -0,0 +1,5 @@
1
+ module S3
2
+ module Migrator
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,68 @@
1
+ require_relative "migrator/version"
2
+
3
+ require "aws-sdk"
4
+
5
+ require "logger"
6
+ require "optparse"
7
+ require "thread"
8
+
9
+ module S3
10
+ module Migrator
11
+ extend self
12
+
13
+ DEFAULT_THREAD_COUNT = 10
14
+
15
+ def migrate!(opts)
16
+ creds = Aws::Credentials.new(opts[:access_key_id], opts[:secret_access_key])
17
+ s3_client = Aws::S3::Client.new(credentials: creds, region: opts[:region])
18
+
19
+ pagerble = if opts[:prefix]
20
+ s3_client.list_objects(bucket: opts[:src_bucket], prefix: opts[:prefix])
21
+ else
22
+ s3_client.list_objects(bucket: opts[:src_bucket])
23
+ end
24
+
25
+ logger = Logger.new(STDOUT)
26
+
27
+ begin
28
+ each_in_threads(opts[:thread_count] || DEFAULT_THREAD_COUNT, pagerble.contents) do |obj|
29
+ begin
30
+ s3_client.copy_object(
31
+ bucket: opts[:dest_bucket],
32
+ key: obj.key,
33
+ copy_source: "#{opts[:src_bucket]}/#{obj.key}",
34
+ acl: "public-read",
35
+ )
36
+ logger.info("copied: #{obj.key}")
37
+ rescue => e
38
+ logger.error(e)
39
+ end
40
+ end
41
+
42
+ is_last = pagerble.last_page?
43
+ pagerble = pagerble.next_page if !is_last
44
+ end while !is_last
45
+ end
46
+
47
+ def each_in_threads(thread_count = 10, enumerable)
48
+ queue = SizedQueue.new(thread_count)
49
+ terminator = { quit: true }
50
+
51
+ threads = thread_count.times.map do
52
+ Thread.new do
53
+ loop do
54
+ obj = queue.shift
55
+ break if obj.is_a?(Hash) && obj[:quit]
56
+ yield obj
57
+ end
58
+ end
59
+ end
60
+
61
+ enumerable.each { |obj| queue.push obj }
62
+ thread_count.times { queue.push terminator }
63
+ threads.each(&:join)
64
+
65
+ enumerable
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 's3/migrator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "s3-migrator"
8
+ spec.version = S3::Migrator::VERSION
9
+ spec.authors = ["yoppi"]
10
+ spec.email = ["y.hirokazu@gmail.com"]
11
+ spec.summary = %q{"Migrate S3 object"}
12
+ spec.description = %q{"Migrate S3 object"}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "aws-sdk", "~> 2"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: s3-migrator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - yoppi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: '"Migrate S3 object"'
56
+ email:
57
+ - y.hirokazu@gmail.com
58
+ executables:
59
+ - s3-migrator
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/s3-migrator
69
+ - lib/s3/migrator.rb
70
+ - lib/s3/migrator/version.rb
71
+ - s3-migrator.gemspec
72
+ homepage: ''
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.4.5
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: '"Migrate S3 object"'
96
+ test_files: []
97
+ has_rdoc: