s3-wrapper 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0ea2f51f491d9026afee758db3e811e7a3e47540
4
+ data.tar.gz: 07507769240ee196fdb98f071b8d86897de6d2fa
5
+ SHA512:
6
+ metadata.gz: 8742a60dcddf948726fd007fe27a134e8e082d7242b1dda4d8a445fd34c89dbb1bd0ac13c8a8b03a24a535b7529b3994dca5ec0ba5549da9dcb51829d9e1a67c
7
+ data.tar.gz: 1b0b97123e6a8bee99187961c83cd4aba431e2097dc9f20c8e2f10d9a403516cf3ce7b134f7394e3642d76c136a9c718f2be102a43cf63441958c05a3790bfd1
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /vendor/bundle
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
11
+ *.bundle
12
+ *.so
13
+ *.o
14
+ *.a
15
+ mkmf.log
16
+ *swp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ bundler_args: --without development --deployment --jobs=3 --retry=3
3
+ cache: bundler
4
+ rvm:
5
+ - 2.2.0
6
+ - 2.1.0
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in s3-wrapper.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'rake'
8
+ gem 'rspec'
9
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Carl
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,40 @@
1
+ # s3-wrapper
2
+
3
+ This gem copies file to s3. It's especially useful to copy backup to s3 in organized structure.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 's3-wrapper'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install s3-wrapper
20
+
21
+ ## Usage
22
+
23
+ Make your profile and upload files to s3.
24
+
25
+ ```bash
26
+ s3-wrapper config-default aws_access_key='aws-access-key' aws_secret_key='aws-secret-key' bucket_name='some-bucket-to-use' host_name='name-of-host-to-backup'
27
+ s3-wrapper create-profile 'profile-name' key1=value1 key2=value2
28
+ s3-wrapper update-profile 'profile-name' key1=value1 key2=value2
29
+ s3-wrapper delete-profile 'profile-name'
30
+
31
+ s3-wrapper upload 'profile-name' file1 file2
32
+ ```
33
+
34
+ ## Contributing
35
+
36
+ 1. Fork it ( https://github.com/carl8899/s3-wrapper/fork )
37
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
38
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
39
+ 4. Push to the branch (`git push origin my-new-feature`)
40
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ begin
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ rescue LoadError
7
+ end
8
+
9
+ task(:default).clear
10
+ task :default => :spec
data/bin/s3-wrapper ADDED
@@ -0,0 +1,39 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 's3-wrapper'
4
+
5
+ command = ARGV.shift
6
+ if command == "config-default"
7
+ params = Hash[ ARGV.join(' ').scan(/([^=\s]+)(?:=(\S+))?/) ]
8
+ profile = S3Wrapper::Profile.config_default(params)
9
+ if profile
10
+ puts "configured default"
11
+ else
12
+ puts "failed to configure default"
13
+ end
14
+ else
15
+ profile_name = ARGV.shift
16
+ profile = S3Wrapper::Profile.get(profile_name)
17
+
18
+ case command
19
+ when "create-profile"
20
+ params = Hash[ ARGV.join(' ').scan(/([^=\s]+)(?:=(\S+))?/) ]
21
+ profile = S3Wrapper::Profile.create(profile_name, params)
22
+ puts "created #{profile.name}" if profile
23
+ when "get-profile"
24
+ if profile
25
+ puts "name: #{profile.name}"
26
+ puts "aws_access_key: #{profile.aws_access_key}"
27
+ puts "aws_secret_key: #{profile.aws_secret_key}"
28
+ puts "bucket_name: #{profile.bucket_name}"
29
+ puts "host_name: #{profile.host_name}"
30
+ end
31
+ when "delete-profile"
32
+ S3Wrapper::Profile.delete(profile.name)
33
+ when "upload"
34
+ if profile
35
+ S3Wrapper.upload_files(profile, ARGV)
36
+ puts "done"
37
+ end
38
+ end
39
+ end
data/lib/s3-wrapper.rb ADDED
@@ -0,0 +1,57 @@
1
+ require "s3-wrapper/version"
2
+ require "s3-wrapper/profile"
3
+ require 'aws-sdk'
4
+
5
+ class S3Wrapper
6
+ attr_accessor :profile
7
+ attr_reader :aws_access_key, :aws_secret_key, :bucket_name, :host_name, :s3
8
+
9
+ def self.upload_files(profile, files)
10
+ wrapper = S3Wrapper.new(profile)
11
+ files.each do |file|
12
+ wrapper.upload_file(file)
13
+ end
14
+ end
15
+
16
+ def initialize(profile)
17
+ self.profile = profile
18
+ AWS.config(access_key_id: aws_access_key, secret_access_key: aws_secret_key, region: 'us-west-2')
19
+ end
20
+
21
+ def aws_access_key
22
+ profile.aws_access_key
23
+ end
24
+
25
+ def aws_secret_key
26
+ profile.aws_secret_key
27
+ end
28
+
29
+ def bucket_name
30
+ profile.bucket_name
31
+ end
32
+
33
+ def host_name
34
+ profile.host_name
35
+ end
36
+
37
+ def profile_name
38
+ profile.name
39
+ end
40
+
41
+ def s3
42
+ @s3 ||= AWS::S3.new()
43
+ end
44
+
45
+ def bucket
46
+ @bucket ||= s3.buckets[bucket_name]
47
+ end
48
+
49
+ def upload_file(file)
50
+ puts "Uploading #{file}"
51
+ today = Time.now.strftime("%Y%m%d")
52
+ key = File.basename(file)
53
+ key = "#{host_name}/#{profile_name}/#{today}/#{key}"
54
+ bucket.objects[key].delete
55
+ bucket.objects[key].write(file: file, acl: :authenticated_read)
56
+ end
57
+ end
@@ -0,0 +1,62 @@
1
+ require 'yaml'
2
+
3
+ class S3Wrapper
4
+ class Profile
5
+ attr_accessor :name, :aws_access_key, :aws_secret_key, :bucket_name, :host_name
6
+ CONFIG_FILE = '.s3-wrapper.rc'
7
+
8
+ def self.config_default(params)
9
+ self::create('default', params)
10
+ end
11
+
12
+ def self.create(name, params)
13
+ config = read_config(name)
14
+ params.each do |key, value|
15
+ config[name][key] = value
16
+ end
17
+ self::write_config(config)
18
+ S3Wrapper::Profile.new(name, config[name])
19
+ end
20
+
21
+ def self.delete(name)
22
+ config = read_config(name)
23
+ config.delete(name)
24
+ self::write_config(config)
25
+ end
26
+
27
+ def self.get(name)
28
+ config = read_config(name)
29
+ S3Wrapper::Profile.new(name, config[name])
30
+ end
31
+
32
+ def initialize(name, params = {})
33
+ self.name = name
34
+ params.each do |key, value|
35
+ instance_variable_set("@#{key}", value)
36
+ end
37
+ end
38
+
39
+ def to_hash
40
+ Hash[*instance_variables.map { |v|
41
+ [v.to_sym, instance_variable_get(v)]
42
+ }.flatten]
43
+ end
44
+
45
+ private
46
+ def self.read_config(name)
47
+ config = {}
48
+ begin
49
+ config = File.open(CONFIG_FILE) { |f| YAML.load(f) }
50
+ rescue
51
+
52
+ end
53
+ config = {} unless config
54
+ config[name] = {} unless config[name]
55
+ config
56
+ end
57
+
58
+ def self.write_config(config)
59
+ open(CONFIG_FILE, 'w+') { |f| YAML.dump(config, f) }
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,3 @@
1
+ class S3Wrapper
2
+ VERSION = "0.1.2"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 's3-wrapper/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "s3-wrapper"
8
+ spec.version = S3Wrapper::VERSION
9
+ spec.authors = ["Carl Blomberg"]
10
+ spec.email = ["carlblomberg@gmail.com"]
11
+ spec.summary = %q{Upload files into AWS S3}
12
+ spec.description = %q{Upload files into AWS S3. It's designed to upload files under predefined hierachy.}
13
+ spec.homepage = "https://github.com/carl8899/s3-wrapper"
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_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_dependency "aws-sdk", "~> 1.54"
24
+ end
@@ -0,0 +1,5 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe S3Wrapper::Profile do
4
+
5
+ end
@@ -0,0 +1,5 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe S3Wrapper do
4
+
5
+ end
@@ -0,0 +1,90 @@
1
+ require File.expand_path("../../lib/s3-wrapper", __FILE__)
2
+ require 'spec_prepare'
3
+
4
+ # This file was generated by the `rspec --init` command. Conventionally, all
5
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
6
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
7
+ # file to always be loaded, without a need to explicitly require it in any files.
8
+ #
9
+ # Given that it is always loaded, you are encouraged to keep this file as
10
+ # light-weight as possible. Requiring heavyweight dependencies from this file
11
+ # will add to the boot time of your test suite on EVERY test run, even for an
12
+ # individual file that may not need all of that loaded. Instead, consider making
13
+ # a separate helper file that requires the additional dependencies and performs
14
+ # the additional setup, and require it from the spec files that actually need it.
15
+ #
16
+ # The `.rspec` file also contains a few flags that are not defaults but that
17
+ # users commonly want.
18
+ #
19
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
20
+ RSpec.configure do |config|
21
+ # rspec-expectations config goes here. You can use an alternate
22
+ # assertion/expectation library such as wrong or the stdlib/minitest
23
+ # assertions if you prefer.
24
+ config.expect_with :rspec do |expectations|
25
+ # This option will default to `true` in RSpec 4. It makes the `description`
26
+ # and `failure_message` of custom matchers include text for helper methods
27
+ # defined using `chain`, e.g.:
28
+ # be_bigger_than(2).and_smaller_than(4).description
29
+ # # => "be bigger than 2 and smaller than 4"
30
+ # ...rather than:
31
+ # # => "be bigger than 2"
32
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
33
+ end
34
+
35
+ # rspec-mocks config goes here. You can use an alternate test double
36
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
37
+ config.mock_with :rspec do |mocks|
38
+ # Prevents you from mocking or stubbing a method that does not exist on
39
+ # a real object. This is generally recommended, and will default to
40
+ # `true` in RSpec 4.
41
+ mocks.verify_partial_doubles = true
42
+ end
43
+
44
+ # The settings below are suggested to provide a good initial experience
45
+ # with RSpec, but feel free to customize to your heart's content.
46
+ # These two settings work together to allow you to limit a spec run
47
+ # to individual examples or groups you care about by tagging them with
48
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
49
+ # get run.
50
+ config.filter_run :focus
51
+ config.run_all_when_everything_filtered = true
52
+
53
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
54
+ # For more details, see:
55
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
56
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
57
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
58
+ config.disable_monkey_patching!
59
+
60
+ # This setting enables warnings. It's recommended, but in some cases may
61
+ # be too noisy due to issues in dependencies.
62
+ config.warnings = true
63
+
64
+ # Many RSpec users commonly either run the entire suite or an individual
65
+ # file, and it's useful to allow more verbose output when running an
66
+ # individual spec file.
67
+ if config.files_to_run.one?
68
+ # Use the documentation formatter for detailed output,
69
+ # unless a formatter has already been configured
70
+ # (e.g. via a command-line flag).
71
+ config.default_formatter = 'doc'
72
+ end
73
+
74
+ # Print the 10 slowest examples and example groups at the
75
+ # end of the spec run, to help surface which specs are running
76
+ # particularly slow.
77
+ config.profile_examples = 10
78
+
79
+ # Run specs in random order to surface order dependencies. If you find an
80
+ # order dependency and want to debug it, you can fix the order by providing
81
+ # the seed, which is printed after each run.
82
+ # --seed 1234
83
+ config.order = :random
84
+
85
+ # Seed global randomization in this process using the `--seed` CLI option.
86
+ # Setting this allows you to use `--seed` to deterministically reproduce
87
+ # test failures related to randomization by passing the same `--seed` value
88
+ # as the one that triggered the failure.
89
+ Kernel.srand config.seed
90
+ end
@@ -0,0 +1,15 @@
1
+ module S3Wrapper::Test
2
+ def self.profile
3
+ S3Wrapper::Profile.get('default')
4
+ end
5
+
6
+ def self.test_connection
7
+ @test_connection ||= AWS::S3.new(access_key_id: profile.aws_access_key, secret_access_key: profile.aws_secret_key)
8
+ end
9
+ end
10
+
11
+ RSpec.configure do |config|
12
+ config.before :suite do
13
+ S3Wrapper::Test.test_connection
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: s3-wrapper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Carl Blomberg
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: aws-sdk
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.54'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.54'
55
+ description: Upload files into AWS S3. It's designed to upload files under predefined
56
+ hierachy.
57
+ email:
58
+ - carlblomberg@gmail.com
59
+ executables:
60
+ - s3-wrapper
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".rspec"
66
+ - ".travis.yml"
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/s3-wrapper
72
+ - lib/s3-wrapper.rb
73
+ - lib/s3-wrapper/profile.rb
74
+ - lib/s3-wrapper/version.rb
75
+ - s3-wrapper.gemspec
76
+ - spec/lib/s3-wrapper/profile_spec.rb
77
+ - spec/lib/s3-wrapper_spec.rb
78
+ - spec/spec_helper.rb
79
+ - spec/spec_prepare.rb
80
+ homepage: https://github.com/carl8899/s3-wrapper
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.4.8
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Upload files into AWS S3
104
+ test_files:
105
+ - spec/lib/s3-wrapper/profile_spec.rb
106
+ - spec/lib/s3-wrapper_spec.rb
107
+ - spec/spec_helper.rb
108
+ - spec/spec_prepare.rb