s3_index 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: a4f63000b5995db2c888de5d831f9093a23c649c
4
+ data.tar.gz: 8b39ba99f902b526f4522b0c4a23a2e16178bbea
5
+ SHA512:
6
+ metadata.gz: ea451266427f5991cedae6aef52c9f7c221d60943b813076557df7a18915be18f88bfc33bda2620e20366bd775b4deaaeaf27ba5e8394896c2eb4fa3c356feb3
7
+ data.tar.gz: c3f659f71fdbf93e7bba0609e12a995e6a384865e5d4ffbba97fe55d1ca7e8a21c293cf95059df23ed0ee3e9db5132c4171709a670bead751206145cd39bd928
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
+ dev.db*
11
+ test.db*
12
+ log/*
13
+ spec/dummy.txt
14
+ .rubocop.yml
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.3
5
+ before_install: gem install bundler -v 1.12.5
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in s3_index.gemspec
4
+ gemspec
5
+
6
+ group :development, :test do
7
+ gem 'pry'
8
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Scott Pierce
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,41 @@
1
+ # S3Index
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/s3_index`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 's3_index'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install s3_index
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/s3_index.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/setup'
2
+ require 'rspec/core/rake_task'
3
+ require 'bundler/gem_tasks'
4
+
5
+
6
+ Dir.glob('lib/tasks/*.rake').each { |r| load r }
7
+
8
+ RSpec::Core::RakeTask.new(:spec)
9
+
10
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "s3_index"
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
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,7 @@
1
+ development:
2
+ adapter: sqlite3
3
+ database: dev.db
4
+
5
+ test:
6
+ adapter: sqlite3
7
+ database: test.db
@@ -0,0 +1,15 @@
1
+ # Create index table
2
+ class InitS3Index < ActiveRecord::Migration
3
+ def change
4
+ create_table :s3_index, force: true do |t|
5
+ t.string :origin_url, index: true
6
+ t.string :file_name, index: true
7
+ t.string :content_type, index: true
8
+ t.string :md5, index: true
9
+ t.integer :size
10
+ t.string :s3_url, index: true, unique: true
11
+ t.string :s3_bucket
12
+ t.timestamps null: false
13
+ end
14
+ end
15
+ end
data/db/schema.rb ADDED
@@ -0,0 +1,34 @@
1
+ # encoding: UTF-8
2
+ # This file is auto-generated from the current state of the database. Instead
3
+ # of editing this file, please use the migrations feature of Active Record to
4
+ # incrementally modify your database, and then regenerate this schema definition.
5
+ #
6
+ # Note that this schema.rb definition is the authoritative source for your
7
+ # database schema. If you need to create the application database on another
8
+ # system, you should be using db:schema:load, not running all the migrations
9
+ # from scratch. The latter is a flawed and unsustainable approach (the more migrations
10
+ # you'll amass, the slower it'll run and the greater likelihood for issues).
11
+ #
12
+ # It's strongly recommended that you check this file into your version control system.
13
+
14
+ ActiveRecord::Schema.define(version: 20161109000001) do
15
+
16
+ create_table "s3_index", force: :cascade do |t|
17
+ t.string "origin_url"
18
+ t.string "file_name"
19
+ t.string "content_type"
20
+ t.string "md5"
21
+ t.integer "size"
22
+ t.string "s3_url"
23
+ t.string "s3_bucket"
24
+ t.datetime "created_at", null: false
25
+ t.datetime "updated_at", null: false
26
+ end
27
+
28
+ add_index "s3_index", ["content_type"], name: "index_s3_index_on_content_type"
29
+ add_index "s3_index", ["file_name"], name: "index_s3_index_on_file_name"
30
+ add_index "s3_index", ["md5"], name: "index_s3_index_on_md5"
31
+ add_index "s3_index", ["origin_url"], name: "index_s3_index_on_origin_url"
32
+ add_index "s3_index", ["s3_url"], name: "index_s3_index_on_s3_url"
33
+
34
+ end
@@ -0,0 +1,15 @@
1
+ # Set up default ENV values
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
+ ENV['S3_INDEX_ENV'] ||= ENV['RAILS_ENV'] || 'development'
4
+
5
+ # Bundler stuff
6
+ require 'rubygems'
7
+ require 'bundler'
8
+ Bundler.setup(:default, ENV['S3_INDEX_ENV'])
9
+
10
+ require 'active_record'
11
+ require 'active_support/all'
12
+ require 'aws-sdk'
13
+ require 'mime-types'
14
+
15
+ ActiveRecord::Base.raise_in_transactional_callbacks = true
@@ -0,0 +1,12 @@
1
+ module S3Index
2
+ # Model containing S3 file metadata.
3
+ # All files uploaded by `S3Index.upload!` will be registered
4
+ # here for easy querying.
5
+ class Index < ActiveRecord::Base
6
+ self.table_name = 's3_index'
7
+
8
+ def download!(**args)
9
+ ::S3Index.download!(index: self, **args)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module S3Index
2
+ VERSION = "0.1.0"
3
+ end
data/lib/s3_index.rb ADDED
@@ -0,0 +1,103 @@
1
+ require 's3_index/version'
2
+ require_relative '../lib/environment'
3
+
4
+ # Namespaces
5
+ module S3Index
6
+ extend ActiveSupport::Autoload
7
+
8
+ autoload :Index
9
+
10
+ module_function
11
+
12
+ def env
13
+ @env ||= ActiveSupport::StringInquirer.new(ENV['S3_INDEX_ENV'])
14
+ end
15
+
16
+ # Use like `Rails.root`
17
+ # @return [Pathname]
18
+ def root
19
+ @root ||= Pathname(File.expand_path('../..', __FILE__))
20
+ end
21
+
22
+ def logger
23
+ @logger ||= ActiveRecord::Base.logger
24
+ end
25
+
26
+ def logger=(logger)
27
+ @logger = logger
28
+ end
29
+
30
+ # Upload file to s3 bucket and register it to the Index model.
31
+ def upload!(s3: default_client, bucket:, src:, dst: src)
32
+ obj = s3.bucket(bucket).object(dst)
33
+
34
+ index = index_for_src(src)
35
+ index_attrs = index_attributes_for(src)
36
+
37
+ # nothing to do, file is registered and the same as before
38
+ return index if index.md5 == index_attrs[:md5]
39
+
40
+ # try to get the data to s3 first, then save in index.
41
+ # order is important, we don't want to make the row until
42
+ # S3 gets the file.
43
+ obj.upload_file(
44
+ src,
45
+ content_type: index_attrs[:content_type],
46
+ content_length: index_attrs[:size]
47
+ )
48
+
49
+ index.s3_url = obj.public_url
50
+ index.s3_bucket = obj.bucket_name
51
+ index.update!(index_attrs)
52
+ index
53
+ end
54
+
55
+ # Download file from s3 bucket back on Index model.
56
+ # src and s3_url are quick options to find an Index model to download.
57
+ def download!(s3: default_client, src: nil, s3_url: nil, index: nil, dst: nil)
58
+ src || s3_url || index ||
59
+ raise(ArgumentError, 'src:, s3_url:, or index: must be provided!')
60
+
61
+ index ||=
62
+ begin
63
+ finder = src ? { origin_url: src } : { s3_url: s3_url }
64
+ Index.find_by(finder) || raise(ActiveRecord::RecordNotFound, "S3Index::Index not found by #{finder}")
65
+ end
66
+
67
+ obj = s3.bucket(index.s3_bucket).object(index.origin_url)
68
+ obj.get(response_target: dst || index.origin_url)
69
+ end
70
+
71
+ def index_for_src(src)
72
+ Index.where(origin_url: src).first_or_initialize
73
+ end
74
+
75
+ def index_attributes_for(path)
76
+ {
77
+ md5: Digest::MD5.file(path).to_s,
78
+ file_name: File.basename(path),
79
+ content_type: MIME::Types.type_for(path).first.try(:content_type),
80
+ size: File.size(path)
81
+ }
82
+ end
83
+
84
+ def default_client
85
+ @s3_client ||= new_client
86
+ end
87
+
88
+ def new_client
89
+ credentials = Aws::SharedCredentials.new(profile_name: 'default')
90
+ client = Aws::S3::Client.new(
91
+ credentials: credentials,
92
+ http_wire_trace: $DEBUG,
93
+ logger: logger
94
+ )
95
+ Aws::S3::Resource.new(client: client)
96
+ end
97
+
98
+ def default_client=(client)
99
+ @s3_client = client
100
+ end
101
+ end
102
+
103
+ S3Index.eager_load!
data/lib/tasks/db.rake ADDED
@@ -0,0 +1,27 @@
1
+ require 's3_index'
2
+
3
+ # Load tasks from ActiveRecord
4
+ spec = Gem::Specification.find_by_name 'activerecord'
5
+ load "#{spec.gem_dir}/lib/active_record/railties/databases.rake"
6
+
7
+ class SeedLoader
8
+ def load_seed
9
+ load 'db/seeds.rb'
10
+ end
11
+ end
12
+
13
+ # @see //gems/activerecord-4.2.5/lib/active_record/tasks/database_tasks.rb
14
+ include ActiveRecord::Tasks
15
+ db_config = YAML.load(ERB.new(File.read('config/database.yml')).result)
16
+
17
+ DatabaseTasks.database_configuration = db_config
18
+ DatabaseTasks.env = S3Index.env
19
+ DatabaseTasks.db_dir = 'db'
20
+ DatabaseTasks.migrations_paths = ['db/migrate']
21
+ DatabaseTasks.root = Dir.pwd
22
+ DatabaseTasks.seed_loader = SeedLoader.new
23
+
24
+ task :environment do
25
+ ActiveRecord::Base.establish_connection(db_config[S3Index.env])
26
+ ActiveRecord::Base.logger = Logger.new(S3Index.root.join('log', "#{S3Index.env}.log"))
27
+ end
data/s3_index.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 's3_index/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 's3_index'
8
+ spec.version = S3Index::VERSION
9
+ spec.authors = ['Scott Pierce']
10
+ spec.email = ['ddrscott@gmail.com']
11
+
12
+ spec.summary = 'Maintain an index of s3 files in the database.'
13
+ spec.homepage = 'https://ddrscott.github.com/s3_index'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = 'exe'
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'activerecord'
22
+ spec.add_dependency 'aws-sdk'
23
+ spec.add_dependency 'mime-types'
24
+
25
+ spec.add_development_dependency 'bundler'
26
+ spec.add_development_dependency 'rake'
27
+ spec.add_development_dependency 'rspec'
28
+ spec.add_development_dependency 'rails'
29
+ spec.add_development_dependency 'rspec-rails'
30
+ spec.add_development_dependency 'sqlite3'
31
+ end
metadata ADDED
@@ -0,0 +1,188 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: s3_index
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Scott Pierce
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-11-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: aws-sdk
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mime-types
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rails
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec-rails
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: sqlite3
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description:
140
+ email:
141
+ - ddrscott@gmail.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".gitignore"
147
+ - ".rspec"
148
+ - ".travis.yml"
149
+ - Gemfile
150
+ - LICENSE.txt
151
+ - README.md
152
+ - Rakefile
153
+ - bin/console
154
+ - bin/setup
155
+ - config/database.yml
156
+ - db/migrate/20161109000001_init_s3_index.rb
157
+ - db/schema.rb
158
+ - lib/environment.rb
159
+ - lib/s3_index.rb
160
+ - lib/s3_index/index.rb
161
+ - lib/s3_index/version.rb
162
+ - lib/tasks/db.rake
163
+ - s3_index.gemspec
164
+ homepage: https://ddrscott.github.com/s3_index
165
+ licenses:
166
+ - MIT
167
+ metadata: {}
168
+ post_install_message:
169
+ rdoc_options: []
170
+ require_paths:
171
+ - lib
172
+ required_ruby_version: !ruby/object:Gem::Requirement
173
+ requirements:
174
+ - - ">="
175
+ - !ruby/object:Gem::Version
176
+ version: '0'
177
+ required_rubygems_version: !ruby/object:Gem::Requirement
178
+ requirements:
179
+ - - ">="
180
+ - !ruby/object:Gem::Version
181
+ version: '0'
182
+ requirements: []
183
+ rubyforge_project:
184
+ rubygems_version: 2.4.5.1
185
+ signing_key:
186
+ specification_version: 4
187
+ summary: Maintain an index of s3 files in the database.
188
+ test_files: []