s3_sync 0.1.0

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: 21b7c36e704bf5137f2d5174392c866c6c40eca9
4
+ data.tar.gz: 77b4b62cbb95ddfb9d4ed8770f59916ffbe8e08f
5
+ SHA512:
6
+ metadata.gz: a97b9662e324210196b7ffe6422cdab8ace7377cb3835f2391a06ecdcbe1149960d59ae765e6e76da13d9261cc7c7a66d675efef1bbb43fa5a653d7449a92ffb
7
+ data.tar.gz: af3c01969d935069f520f54270afa510d01dfb93e60a885687c17b309658313d58d6eb024b6f3a9823d4481db11929917f7ff1b3d2da8092ce2886226de4b5d5
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
4
+ before_install: gem install bundler -v 1.10.5
data/CREDITS.md ADDED
@@ -0,0 +1,7 @@
1
+ # Credits, Notes, and Reference
2
+ + http://stackoverflow.com/questions/2889720/one-liner-in-ruby-for-displaying-a-prompt-getting-input-and-assigning-to-a-var
3
+ + https://robots.thoughtbot.com/mygem-configure-block
4
+ + http://brandonhilkert.com/blog/ruby-gem-configuration-patterns/
5
+ + http://stackoverflow.com/questions/10584638/setting-up-configuration-settings-when-writing-a-gem
6
+ + http://docs.aws.amazon.com/sdkforruby/api/index.html
7
+ +
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in s3_sync.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 MJ Rossetti (@s2t2)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,111 @@
1
+ # s3_sync
2
+
3
+ Securely sync (upload and download) files with [Amazon Simple Storage Service (s3)](http://aws.amazon.com/s3).
4
+
5
+ Specify credentials, file names, and other options during configuration.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 's3_sync'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install s3_sync
22
+
23
+ ## Usage
24
+
25
+ ### Configuring
26
+
27
+ Configure the gem to use the same options when uploading and downloading.
28
+
29
+ ```` rb
30
+ S3Sync.configure do |config|
31
+ config.key_id = "mykey123"
32
+ config.key_secret = "secret456"
33
+ config.region = "us-east-1"
34
+ config.bucket = "my-backups"
35
+ config.secret_phrase = "my-s3cr3t"
36
+ config.files = [
37
+ File.join(Dir.home,".bash_profile"),
38
+ File.join(Dir.home,".gitconfig"),
39
+ File.join(Dir.home,".ssh","config")
40
+ ]
41
+ config.downloads_dir = File.join(Dir.home,"Desktop","my-s3-downloads")
42
+ end
43
+ ````
44
+
45
+ #### Configuration Options
46
+
47
+ attribute name | description | default value
48
+ --- | --- | ---
49
+ `key_id` | The s3 user's *Access Key Id*. | N/A
50
+ `key_secret` | The s3 user's *Access Key Secret*. | N/A
51
+ `region` | The s3 region. | N/A
52
+ `bucket` | The s3 bucket (top-level directory) name. | N/A
53
+ `secret_phrase` | The phrase to use when encrypting and decrypting files. | N/A
54
+ `files` | A list of local file paths to be synced. | N/A
55
+ `downloads_dir` | A staging directory to house downloaded files. | `File.join(Dir.home,"Desktop","s3-downloads")`
56
+
57
+ ### Uploading
58
+
59
+ Upload files from your computer to s3.
60
+
61
+ ```` rb
62
+ S3Sync::Upload.new
63
+ ````
64
+
65
+ The uploads bucket gets created automatically, and can be configured with `config.bucket`.
66
+
67
+ ### Downloading
68
+
69
+ Download files from s3 to your computer.
70
+
71
+ ```` rb
72
+ S3Sync::Download.new
73
+ ````
74
+
75
+ Files are downloaded to a staging directory, which can be configured with `config.downloads_dir`. The staging directory helps mitigate the risk of accidentally over-writing local files.
76
+
77
+ ## Prerequisites
78
+
79
+ Create an [AWS Identity and Access Management (IAM)](http://aws.amazon.com/iam/) user and obtain its *Access Key Id* and *Secret Access Key*.
80
+
81
+ Create an AWS IAM group.
82
+
83
+ Create an AWS IAM policy with the *arn:aws:iam::aws:policy/AmazonS3FullAccess* permission set.
84
+
85
+ Attach the policy to the group.
86
+
87
+ Add the user to the group.
88
+
89
+ ## Contributing
90
+
91
+ Browse [existing issues](https://github.com/s2t2/s3-sync-ruby/issues) or create a [new issue](https://github.com/s2t2/s3-sync-ruby/issues/new) to communicate bugs, desired features, etc.
92
+
93
+ After forking the repo and pushing your changes, create a [pull request](https://github.com/s2t2/s3-sync-ruby/pulls/new) referencing the applicable issue(s).
94
+
95
+ ### Installation
96
+
97
+ Check out the repo with `git clone git@github.com:s2t2/s3-sync-ruby.git`, and `cd s3-sync-ruby`.
98
+
99
+ After checking out the repo, run `bin/setup` to install dependencies.
100
+
101
+ ### Testing
102
+
103
+ Run `bundle exec rake` or `bundle exec rspec spec/` to run the tests.
104
+
105
+ You can also run `bin/console` for an interactive prompt that will allow you to experiment.
106
+
107
+ To install this gem onto your local machine, run `bundle exec rake install`.
108
+
109
+ ### Releasing
110
+
111
+ Update the version number in `version.rb`, then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "s3_sync"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ require "pry"
11
+ Pry.start
12
+
13
+ # require "irb"
14
+ # IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,26 @@
1
+ module S3Sync
2
+ class Configuration
3
+ attr_accessor :key_id, :key_secret, :region, :bucket, :secret_phrase, :downloads_dir, :files
4
+
5
+ # @param [Hash] options
6
+ # @option options [String] :key_id The "Access Key Id" for your s3 user
7
+ # @option options [String] :key_secret The "Access Key Secret" for your s3 user
8
+ # @option options [String] :region The s3 region
9
+ # @option options [String] :bucket The s3 bucket (top-level directory) name
10
+ # @option options [String] :secret_phrase The encryption phrase to use when uploading (encrypting) and downloading (decrypting) files
11
+ # @option options [Array] :downloads_dir A staging directory to house files downloaded from s3
12
+ # @option options [Array] :files A list of local file paths
13
+ #
14
+ # @example
15
+ # S3Sync::Configuration.new(:key_id => "mykey123", :key_secret => "secret456", :region => "us-east-1", :bucket => "my-backups", :files => [".bash_profile",".gitconfig"], :secret_phrase => "my-s3cr3t")
16
+ def initialize(options = {})
17
+ @key_id = options[:key_id]
18
+ @key_secret = options[:key_secret]
19
+ @secret_phrase = options[:secret_phrase]
20
+ @bucket = options[:bucket]
21
+ @region = options[:region]
22
+ @downloads_dir = options[:downloads_dir] || File.join(Dir.home,"Desktop","s3-downloads")
23
+ @files = options[:files]
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,47 @@
1
+ module S3Sync
2
+ class Download
3
+ attr_accessor :key_id, :key_secret, :region, :bucket, :secret_phrase, :downloads_dir, :files
4
+
5
+ def initialize(options = S3Sync.configuration)
6
+ @key_id = options.key_id
7
+ @key_secret = options.key_secret
8
+ @region = options.region
9
+ @bucket = options.bucket
10
+ @secret_phrase = options.secret_phrase
11
+ @files = options.files
12
+ @downloads_dir = options.downloads_dir
13
+ raise "check your configuration" unless [@key_id, @key_secret, @region, @bucket, @secret_phrase, @downloads_dir].map{|s| s.class}.uniq == [String] && @files.is_a?(Array)
14
+
15
+ creds = Aws::Credentials.new(@key_id, @key_secret)
16
+ client = Aws::S3::Client.new(:region => @region, :credentials => creds)
17
+
18
+ # Find latest uploads directory.
19
+
20
+ object_keys = []
21
+ objects = client.list_objects(:bucket => @bucket)
22
+ while objects.last_page? == false
23
+ object_keys << objects.contents.map{|obj| obj.key}
24
+ objects = objects.next_page
25
+ end
26
+ object_directories = object_keys.flatten.map{|str| str.split("/").first}.compact.uniq
27
+ days = object_directories.map{|str| Date.parse(str) rescue nil}.compact
28
+ latest_day = days.max.to_s
29
+
30
+ # Create downloads directory.
31
+
32
+ FileUtils.mkdir_p(@downloads_dir)
33
+
34
+ # Download files.
35
+
36
+ files.each do |file|
37
+ s3_file = File.join(latest_day, file)
38
+ local_file = File.join(@downloads_dir, @bucket, file)
39
+ FileUtils.mkdir_p(local_file.gsub(local_file.split("/").last,""))
40
+ client.get_object({:bucket => @bucket, :key => s3_file}, target: local_file)
41
+ decrypted_file_contents = Encryptor.decrypt(File.read(local_file), :key => secret_phrase)
42
+ File.write(local_file, decrypted_file_contents)
43
+ puts "downloaded and decrypted #{@bucket}/#{s3_file} to #{local_file}"
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,35 @@
1
+ module S3Sync
2
+ class Upload
3
+ attr_accessor :key_id, :key_secret, :region, :bucket, :secret_phrase, :files
4
+
5
+ def initialize(options = S3Sync.configuration)
6
+ @key_id = options.key_id
7
+ @key_secret = options.key_secret
8
+ @region = options.region
9
+ @bucket = options.bucket
10
+ @secret_phrase = options.secret_phrase
11
+ @files = options.files
12
+ raise "check your configuration" unless [@key_id, @key_secret, @region, @bucket, @secret_phrase].map{|s| s.class}.uniq == [String] && @files.is_a?(Array)
13
+
14
+ creds = Aws::Credentials.new(@key_id, @key_secret)
15
+
16
+ # Create bucket.
17
+
18
+ resource = Aws::S3::Resource.new(:region => @region, credentials: creds)
19
+ bucket_names = resource.buckets.map{|b| b.name}
20
+ resource.create_bucket(:bucket => @bucket, :acl => "private") unless bucket_names.include?(@bucket)
21
+
22
+ # Upload files
23
+
24
+ client = Aws::S3::Client.new(:region => @region, :credentials => creds)
25
+
26
+ @files.each do |file|
27
+ raise "couldn't find file #{file}" unless File.exist?(file)
28
+ encrypted_file_contents = Encryptor.encrypt(File.read(file), :key => @secret_phrase)
29
+ s3_file = File.join(Date.today.to_s, file)
30
+ client.put_object(:bucket => @bucket, :key => s3_file, :body => encrypted_file_contents)
31
+ puts "uploaded encrypted contents of #{file} to #{@bucket}/#{s3_file}"
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module S3Sync
2
+ VERSION = "0.1.0"
3
+ end
data/lib/s3_sync.rb ADDED
@@ -0,0 +1,21 @@
1
+ require "aws-sdk"
2
+ require "encryptor"
3
+
4
+ require "s3_sync/configuration"
5
+ require "s3_sync/download"
6
+ require "s3_sync/upload"
7
+ require "s3_sync/version"
8
+
9
+ module S3Sync
10
+ class << self
11
+ attr_writer :configuration
12
+ end
13
+
14
+ def self.configuration
15
+ @configuration ||= S3Sync::Configuration.new
16
+ end
17
+
18
+ def self.configure
19
+ yield(configuration) #if block_given?
20
+ end
21
+ end
data/s3_sync.gemspec ADDED
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 's3_sync/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "s3_sync"
8
+ spec.version = S3Sync::VERSION
9
+ spec.authors = ["MJ Rossetti (@s2t2)"]
10
+ spec.email = ["s2t2mail+git@gmail.com"]
11
+
12
+ spec.summary = %q{Securely sync (upload and download) files with Amazon Simple Storage Service (s3).}
13
+ spec.description = %q{Securely sync (upload and download) files with Amazon Simple Storage Service (s3). Specify credentials, file names, and other options during configuration.}
14
+
15
+ spec.homepage = "https://github.com/s2t2/s3-sync-ruby"
16
+ spec.license = "MIT"
17
+
18
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
19
+ # delete this section to allow pushing this gem to any host.
20
+ ### if spec.respond_to?(:metadata)
21
+ ### spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
22
+ ### else
23
+ ### raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
24
+ ### end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_development_dependency "bundler", "~> 1.10"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ spec.add_development_dependency "rspec", "~> 3.1"
34
+ spec.add_development_dependency "pry", "~> 0.10"
35
+ spec.add_dependency 'aws-sdk', '~> 2'
36
+ spec.add_dependency 'encryptor'
37
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: s3_sync
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - MJ Rossetti (@s2t2)
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-21 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.10'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.10'
69
+ - !ruby/object:Gem::Dependency
70
+ name: aws-sdk
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2'
83
+ - !ruby/object:Gem::Dependency
84
+ name: encryptor
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Securely sync (upload and download) files with Amazon Simple Storage
98
+ Service (s3). Specify credentials, file names, and other options during configuration.
99
+ email:
100
+ - s2t2mail+git@gmail.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - ".travis.yml"
108
+ - CREDITS.md
109
+ - Gemfile
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - bin/console
114
+ - bin/setup
115
+ - lib/s3_sync.rb
116
+ - lib/s3_sync/configuration.rb
117
+ - lib/s3_sync/download.rb
118
+ - lib/s3_sync/upload.rb
119
+ - lib/s3_sync/version.rb
120
+ - s3_sync.gemspec
121
+ homepage: https://github.com/s2t2/s3-sync-ruby
122
+ licenses:
123
+ - MIT
124
+ metadata: {}
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ requirements: []
140
+ rubyforge_project:
141
+ rubygems_version: 2.4.5
142
+ signing_key:
143
+ specification_version: 4
144
+ summary: Securely sync (upload and download) files with Amazon Simple Storage Service
145
+ (s3).
146
+ test_files: []