has_s3_attachment 0.1.1
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 +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/README.md +92 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/has_s3_attachment.gemspec +27 -0
- data/lib/has_s3_attachment/version.rb +3 -0
- data/lib/has_s3_attachment.rb +131 -0
- metadata +124 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: bf3fce12bdcf3867528e6c4d5ac62bdd62032623
|
4
|
+
data.tar.gz: 66a1e9b3a64c0e0e5a79cc8325e1ffe8c8a6f643
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 05ff359f71fcf101cf8208d06c5c3a35411d57cef0023cc5b13166be1851cd6680b78bad49d03bb7c078ec0bfe08532fb277a1381a1f7f70e1e1b0ce6c480529
|
7
|
+
data.tar.gz: 2b44b7dfa30cee2ad77eea7fd27951d9f877bfe4c9e30d0f0a76510cb9442ab03e0d274ef08762761ffe787c06022ab178624d46d4e489f3f741f8a4d7fd8c50
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
# HasS3Attachment
|
2
|
+
|
3
|
+
A simple gem, which manages Rails model file attachments hosted on *aws s3*.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
Typical use case is when you have single paged web app backed by Rails app,
|
7
|
+
and you upload your attachments right from the browser to **s3** via `CORS` -
|
8
|
+
as a result you have only location of your file on **s3**. Then you may wanna link this attachment to some model in your app:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
class VideoReport < ActiveRecord::Base
|
12
|
+
include HasS3Attachment
|
13
|
+
|
14
|
+
has_s3_attachment(
|
15
|
+
:video,
|
16
|
+
s3_options: {
|
17
|
+
region: 'us-west-2',
|
18
|
+
key: 'key-xxx',
|
19
|
+
secret: 'secret-xxx'
|
20
|
+
},
|
21
|
+
host_alias: 'cdn-example.com'
|
22
|
+
)
|
23
|
+
end
|
24
|
+
```
|
25
|
+
To get it working you must have `s3_bucket_paths` attr of type `string` declared on your model.
|
26
|
+
Set **s3** location on an instance of your model as following (to associate your model with attachment):
|
27
|
+
```ruby
|
28
|
+
video_report.video_s3_bucket = 'your_bucket'
|
29
|
+
video_report.video_s3_path = '/path/to/file.mp4'
|
30
|
+
```
|
31
|
+
Then you'll be able to to call the next methods:
|
32
|
+
```ruby
|
33
|
+
video_report.video_url # returns full attachment url
|
34
|
+
video_report.delete_video # removes attachment from s3, may be used in conjunction with `before_destroy` ActiveRecord callback
|
35
|
+
video_report.open_video do |f|
|
36
|
+
# f is an input stream of your attachment
|
37
|
+
end
|
38
|
+
```
|
39
|
+
## Multiple attachments, one model
|
40
|
+
You can add multiple attachments to your model like this:
|
41
|
+
```ruby
|
42
|
+
class User < ActiveRecord::Base
|
43
|
+
has_s3_attachment(
|
44
|
+
:photo,
|
45
|
+
s3_options: {
|
46
|
+
region: 'us-west-2',
|
47
|
+
key: 'key-xxx',
|
48
|
+
secret: 'secret-xxx'
|
49
|
+
},
|
50
|
+
host_alias: 'cdn-example.com'
|
51
|
+
)
|
52
|
+
|
53
|
+
has_s3_attachment(
|
54
|
+
:document,
|
55
|
+
s3_options: {
|
56
|
+
region: 'us-west-1',
|
57
|
+
key: 'key-xxx1',
|
58
|
+
secret: 'secret-xxx'
|
59
|
+
}
|
60
|
+
)
|
61
|
+
end
|
62
|
+
```
|
63
|
+
|
64
|
+
## Installation
|
65
|
+
|
66
|
+
Add this line to your application's Gemfile:
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
gem 'has_s3_attachment'
|
70
|
+
```
|
71
|
+
|
72
|
+
And then execute:
|
73
|
+
|
74
|
+
$ bundle
|
75
|
+
|
76
|
+
Or install it yourself as:
|
77
|
+
|
78
|
+
$ gem install has_s3_attachment
|
79
|
+
|
80
|
+
## Development
|
81
|
+
|
82
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
83
|
+
|
84
|
+
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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
85
|
+
|
86
|
+
## Contributing
|
87
|
+
|
88
|
+
1. Fork it ( https://github.com/pavloo/has_s3_attachment/fork )
|
89
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
90
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
91
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
92
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "has_s3_attachment"
|
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,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'has_s3_attachment/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "has_s3_attachment"
|
8
|
+
spec.version = HasS3Attachment::VERSION
|
9
|
+
spec.authors = ["Pavlo Osadchyi"]
|
10
|
+
spec.email = ["posadchiy@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Gently wraps you s3 resource urls into ActiveModel::Record}
|
13
|
+
spec.description = %q{Gently wraps you s3 resource urls into ActiveModel::Record}
|
14
|
+
spec.homepage = "http://github.com/pavloo/has_s3_attachment"
|
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_development_dependency "bundler", "~> 1.9"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec", "~> 3.2.0"
|
24
|
+
spec.add_development_dependency "webmock", "~> 1.21.0"
|
25
|
+
|
26
|
+
spec.add_runtime_dependency "aws-sdk", "~> 2"
|
27
|
+
end
|
@@ -0,0 +1,131 @@
|
|
1
|
+
require "has_s3_attachment/version"
|
2
|
+
require "aws-sdk"
|
3
|
+
require "open-uri"
|
4
|
+
require "json"
|
5
|
+
|
6
|
+
module HasS3Attachment
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def has_s3_attachment(attachment_name, options)
|
10
|
+
@attachment_name = attachment_name
|
11
|
+
set_s3_options(attachment_name, options[:s3_options])
|
12
|
+
host_alias = options[:host_alias]
|
13
|
+
|
14
|
+
define_method("#{attachment_name}_url".to_sym) do |*args|
|
15
|
+
s3_bucket = send :"#{attachment_name}_s3_bucket"
|
16
|
+
s3_path = send :"#{attachment_name}_s3_path"
|
17
|
+
host = send :"#{attachment_name}_host_alias"
|
18
|
+
host_alias = host || host_alias
|
19
|
+
if host_alias
|
20
|
+
ssl = args[0] && args[0].key?(:ssl) ? args[0][:ssl] : true
|
21
|
+
url_str = URI::HTTP.build(
|
22
|
+
host: host_alias,
|
23
|
+
path: s3_path
|
24
|
+
).to_s
|
25
|
+
|
26
|
+
url_str.gsub!(/http/, 'https') if ssl
|
27
|
+
|
28
|
+
url_str
|
29
|
+
else
|
30
|
+
@s3[attachment_name].bucket(s3_bucket).object(s3_path[1..-1]).public_url if s3_path && s3_bucket
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
define_method("delete_#{attachment_name}".to_sym) do
|
36
|
+
s3_bucket = send :"#{attachment_name}_s3_bucket"
|
37
|
+
s3_path = send :"#{attachment_name}_s3_path"
|
38
|
+
@s3[attachment_name].bucket(s3_bucket).object(s3_path[1..-1]).delete
|
39
|
+
end
|
40
|
+
|
41
|
+
define_method(:"open_#{attachment_name}") do |&block|
|
42
|
+
url = send :"#{attachment_name}_url"
|
43
|
+
open(url) do |f|
|
44
|
+
block.call f
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
define_method(:"#{attachment_name}_s3_bucket=") do |bucket|
|
49
|
+
set_s3_paths(attachment_name, :bucket, bucket)
|
50
|
+
end
|
51
|
+
|
52
|
+
define_method(:"#{attachment_name}_host_alias=") do |host_alias|
|
53
|
+
set_s3_paths(attachment_name, :host_alias, host_alias)
|
54
|
+
end
|
55
|
+
|
56
|
+
define_method(:"#{attachment_name}_s3_path=") do |path|
|
57
|
+
fail 's3_path must be absolute and start with "/"' unless path.start_with?('/')
|
58
|
+
set_s3_paths(attachment_name, :path, path)
|
59
|
+
end
|
60
|
+
|
61
|
+
define_method(:"#{attachment_name}_s3_bucket") do
|
62
|
+
get_s3_paths(attachment_name, :bucket)
|
63
|
+
end
|
64
|
+
|
65
|
+
define_method(:"#{attachment_name}_s3_path") do
|
66
|
+
get_s3_paths(attachment_name, :path)
|
67
|
+
end
|
68
|
+
|
69
|
+
define_method(:"#{attachment_name}_host_alias") do
|
70
|
+
get_s3_paths(attachment_name, :host_alias)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def set_s3_options(attachment_name, options)
|
75
|
+
@s3_options = {} unless @s3_options
|
76
|
+
@s3_options[attachment_name] = options
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def self.included(base)
|
81
|
+
base.extend ClassMethods
|
82
|
+
class << base
|
83
|
+
attr_reader :s3_options, :host_alias, :attachment_name
|
84
|
+
end
|
85
|
+
|
86
|
+
base.after_find :init if base.respond_to?(:after_find)
|
87
|
+
end
|
88
|
+
|
89
|
+
def initialize(*args)
|
90
|
+
super
|
91
|
+
init
|
92
|
+
end
|
93
|
+
|
94
|
+
private
|
95
|
+
|
96
|
+
def set_s3_paths(name, type, value)
|
97
|
+
begin
|
98
|
+
hash = s3_bucket_paths ? JSON.parse(s3_bucket_paths) : Hash.new
|
99
|
+
rescue NameError => e
|
100
|
+
raise NameError, 'You must create model attribute "s3_bucket_paths" of type string in order to make has_s3_attachment work'
|
101
|
+
end
|
102
|
+
hash["#{name}"] = {} unless hash.key? "#{name}"
|
103
|
+
hash["#{name}"][type.to_s] = value
|
104
|
+
self.s3_bucket_paths = JSON.generate(hash)
|
105
|
+
end
|
106
|
+
|
107
|
+
def get_s3_paths(name, type)
|
108
|
+
begin
|
109
|
+
hash = s3_bucket_paths ? JSON.parse(s3_bucket_paths) : Hash.new
|
110
|
+
rescue NameError => e
|
111
|
+
raise NameError, 'You must create model attribute "s3_bucket_paths" of type string in order to make has_s3_attachment work'
|
112
|
+
end
|
113
|
+
hash["#{name}"][type.to_s] if hash.key? "#{name}"
|
114
|
+
end
|
115
|
+
|
116
|
+
def init
|
117
|
+
s3_options = self.class.s3_options
|
118
|
+
@s3 = {}
|
119
|
+
s3_options.keys.each do |name|
|
120
|
+
options = s3_options[name]
|
121
|
+
if options
|
122
|
+
@s3[name] = Aws::S3::Resource.new(
|
123
|
+
region: options[:region],
|
124
|
+
credentials: Aws::Credentials.new(options[:key], options[:secret])
|
125
|
+
)
|
126
|
+
else
|
127
|
+
@s3[name] = Aws::S3::Client.new
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
metadata
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: has_s3_attachment
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Pavlo Osadchyi
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-06-05 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.9'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.9'
|
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.2.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.2.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: webmock
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.21.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 1.21.0
|
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
|
+
description: Gently wraps you s3 resource urls into ActiveModel::Record
|
84
|
+
email:
|
85
|
+
- posadchiy@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
|
+
- Gemfile
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- bin/console
|
97
|
+
- bin/setup
|
98
|
+
- has_s3_attachment.gemspec
|
99
|
+
- lib/has_s3_attachment.rb
|
100
|
+
- lib/has_s3_attachment/version.rb
|
101
|
+
homepage: http://github.com/pavloo/has_s3_attachment
|
102
|
+
licenses: []
|
103
|
+
metadata: {}
|
104
|
+
post_install_message:
|
105
|
+
rdoc_options: []
|
106
|
+
require_paths:
|
107
|
+
- lib
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
requirements: []
|
119
|
+
rubyforge_project:
|
120
|
+
rubygems_version: 2.2.2
|
121
|
+
signing_key:
|
122
|
+
specification_version: 4
|
123
|
+
summary: Gently wraps you s3 resource urls into ActiveModel::Record
|
124
|
+
test_files: []
|