aws_upload 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d4ed9e16752353b0364e38bb1f33a7f88b78768c
4
+ data.tar.gz: 620f6c695c65b5c2ca1c698d7f6a414b82c62019
5
+ SHA512:
6
+ metadata.gz: b6e9df787666bd36157334fe262233d8b882bad57364f1ec2d6e22bf5ef4708d32c1a872cc70d2bff509270476d5e02c1c46bc6071da5585564a22220552e753
7
+ data.tar.gz: b24e2ce43c541669a9cf167c9b6676365c60deedd7d89a54f05c6372a0beeaf9fb8fbe6ef7a56b26a8c2614059964f4b6cbecb8a0f8247015d629e92d9f2eeb9
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in aws_upload.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 ykmr1224
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.
@@ -0,0 +1,69 @@
1
+ # AwsUpload
2
+
3
+ This gem offers a helper method to build a form HTML to upload directry to Amazon S3 storage by using POST method.
4
+ With this gem, you can easily exclude settings for aws s3 direct upload and generate upload form by calling a single helper method.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'aws_upload'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install aws_upload
19
+
20
+ ## Usage
21
+
22
+ Make your initialization script to configure uploading parameters.
23
+
24
+ in config/initializers/aws_uplaod.rb
25
+ ```ruby
26
+ AwsUpload.configure do |config|
27
+ config.aws_access_key_id = ENV['AWS_ACCESS_KEY_ID']
28
+ config.aws_secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']
29
+ config.aws_region = ENV['AWS_REGION']
30
+
31
+ config.uploader :image do |u|
32
+ u.bucket = 'bucket-name'
33
+ u.key_prefix = 'some-prefix/'
34
+ u.options[:acl] = "public-read"
35
+ u.options[:content_length_range] = 0..1024
36
+ u.options[:success_action_redirect] = "http://url.to/redirected/pass"
37
+ end
38
+ end
39
+ ```
40
+
41
+ Call aws_upload_form method from view erb file.
42
+
43
+ ```erb
44
+ Generate a form to upload a file with the name of "uploaded_file.jpg"
45
+ <%= aws_upload_form(:image, "uploaded_file.jpg") %>
46
+
47
+ Generate a form to upload a file with the uploaded file name
48
+ <%= aws_upload_form(:image, "${filename}") %>
49
+
50
+ You can specify options. It will override the options specified in the configuration.
51
+ <%= aws_upload_form(:image, "uploaded_file.jpg", acl: "private", metadata: {original-filename: '${filename'}) %>
52
+
53
+ You can customize file and submit input by giving a block.
54
+ <%= aws_upload_form(:image, "uploaded_file.jpg") do %>
55
+ <input type="file" name="file">
56
+ <input type="submit" value="Upload">
57
+ <% end %>
58
+ ```
59
+
60
+ You can find further options in AWS::S3::PresignedPost document, which is internaly used in this gem.
61
+ http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/S3/PresignedPost.html#initialize-instance_method
62
+
63
+ ## Contributing
64
+
65
+ 1. Fork it
66
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
67
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
68
+ 4. Push to the branch (`git push origin my-new-feature`)
69
+ 5. Create new Pull Request
@@ -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
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'aws_upload/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "aws_upload"
8
+ spec.version = AwsUpload::VERSION
9
+ spec.authors = ["ykmr1224"]
10
+ spec.email = ["ykmr1224@gmail.com"]
11
+ spec.description = %q{This gem offers a helper method to build a form HTML to upload directry to Amazon S3 storage by using POST method.}
12
+ spec.summary = %q{This gem offers a helper method to build a form HTML to upload directry to Amazon S3 storage by using POST method.}
13
+ spec.homepage = "https://github.com/ykmr1224"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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"
22
+ spec.add_dependency "activesupport"
23
+ spec.add_dependency "actionview"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.3"
26
+ spec.add_development_dependency "rake"
27
+ spec.add_development_dependency "rspec"
28
+ end
@@ -0,0 +1,68 @@
1
+ require "aws_upload/version"
2
+ require 'aws_upload/form_helper.rb'
3
+ require 'aws-sdk'
4
+
5
+ module AwsUpload
6
+
7
+ ##
8
+ # Module method to configure AwsUpload
9
+ # === Examples
10
+ # AwsUpload.configure do |config|
11
+ # config.aws_access_key_id = ENV['AWS_ACCESS_KEY_ID']
12
+ # config.aws_secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']
13
+ # config.aws_region = ENV['AWS_REGION']
14
+ #
15
+ # config.uploader :image do |u|
16
+ # u.bucket = 'bucket-name'
17
+ # u.key_prefix = 'some-prefix/'
18
+ # u.options[:acl] = "public-read"
19
+ # u.options[:content_length_range] = 0..1024
20
+ # u.options[:success_action_redirect] = "http://url.to/redirected/pass"
21
+ # end
22
+ #
23
+ # config.uploader :csv do |u|
24
+ # u.bucket = 'bucket-name'
25
+ # u.key_prefix = 'some-prefix/'
26
+ # u.options[:acl] = "private"
27
+ # u.options[:content_length_range] = 0..10240
28
+ # end
29
+ # end
30
+ def self.configure
31
+ yield self
32
+ end
33
+
34
+ # Access key id for upload
35
+ mattr_accessor :aws_access_key_id
36
+ @@aws_access_key_id = nil
37
+
38
+ # Secret access key for upload
39
+ mattr_accessor :aws_secret_access_key
40
+ @@aws_secret_access_key = nil
41
+
42
+ # S3 bucket region
43
+ mattr_accessor :aws_region
44
+ @@aws_region = ENV['AWS_REGION']
45
+
46
+ @@uploaders = {}
47
+
48
+ def self.uploader(*args, &block)
49
+ if block_given?
50
+ options = args.extract_options!
51
+ name = args.first || :default
52
+ @@uploaders[name] = Uploader.new().tap{ |u| yield(u) }
53
+ else
54
+ @@uploaders[args.first || :default]
55
+ end
56
+ end
57
+
58
+ # Uploader instance will holds upload settings
59
+ class Uploader
60
+ attr_accessor :options, :key_prefix, :bucket
61
+
62
+ def initialize
63
+ @options = {}
64
+ @key_prefx = ""
65
+ @bucket = nil
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,60 @@
1
+ require 'action_view'
2
+
3
+ module AwsUpload
4
+ ##
5
+ # This module creates aws direct upload form helper
6
+ # === Examples
7
+ # Generate a form to upload a file with the name of "uploaded_file.jpg"
8
+ # <%= aws_upload_form(:image, "uploaded_file.jpg") %>
9
+ #
10
+ # Generate a form to upload a file with the uploaded file name
11
+ # <%= aws_upload_form(:image, "${filename}") %>
12
+ #
13
+ # You can specify options. It will override the options specified in the configuration.
14
+ # <%= aws_upload_form(:image, "uploaded_file.jpg", acl: "private", metadata: {original-filename: '${filename'}) %>
15
+ #
16
+ # You can customize file and submit input by giving a block.
17
+ # <%= aws_upload_form(:image, "uploaded_file.jpg") do %>
18
+ # <input type="file" name="file">
19
+ # <input type="submit" value="Upload">
20
+ # <% end %>
21
+ #
22
+ module FormHelper
23
+ extend self
24
+
25
+ def aws_upload_form(name, object_key, options={})
26
+ # Load credentials
27
+ if AwsUpload.aws_access_key_id
28
+ creds = Aws::Credentials.new(AwsUpload.aws_access_key_id, AwsUpload.aws_secret_access_key)
29
+ else
30
+ # Use shared credentials if aws_access_key_id is not specified
31
+ creds = Aws::SharedCredentials.new()
32
+ end
33
+
34
+ region = AwsUpload.aws_region
35
+
36
+ uploader = AwsUpload.uploader(name)
37
+ opts = uploader.options.deep_merge(options)
38
+ opts[:key] = uploader.key_prefix + object_key
39
+
40
+ post = Aws::S3::PresignedPost.new(creds, region, uploader.bucket, opts)
41
+ form = "<form action='#{post.url.to_s}' method='post' enctype='multipart/form-data'>\n"
42
+ post.fields.each do |key, value|
43
+ form << "\t<input type='hidden' name='#{key}' value='#{value}'>\n"
44
+ end
45
+
46
+ # render file and submit inputs. yield block if a block is given.
47
+ if block_given?
48
+ form << yield
49
+ else
50
+ form << "\t<input type='file' name='file'>\n"
51
+ form << "\t<input type='submit' value='upload'>\n"
52
+ end
53
+
54
+ form << "</form>"
55
+ form.html_safe
56
+ end
57
+ end
58
+ end
59
+
60
+ ActionView::Base.send :include, AwsUpload::FormHelper
@@ -0,0 +1,3 @@
1
+ module AwsUpload
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,78 @@
1
+ require 'spec_helper'
2
+
3
+ describe AwsUpload do
4
+ it 'has a version number' do
5
+ expect(AwsUpload::VERSION).not_to be_nil
6
+ end
7
+
8
+ before(:all) do
9
+ AwsUpload::configure do |config|
10
+ config.aws_access_key_id = "some_key"
11
+ config.aws_secret_access_key = "some_secret"
12
+ config.aws_region = "us-west-2"
13
+
14
+ #default
15
+ config.uploader do |u|
16
+ u.bucket = 'default-bucket'
17
+ u.key_prefix = 'some-prefix/'
18
+ u.options[:acl] = "private"
19
+ u.options[:content_length_range] = 0..10240
20
+ end
21
+
22
+ #named uploader
23
+ config.uploader :image do |u|
24
+ u.bucket = 'image-bucket'
25
+ u.key_prefix = 'img-'
26
+ u.options[:acl] = "public-read"
27
+ u.options[:content_length_range] = 0..1024
28
+ u.options[:success_action_redirect] = "http://url.to/redirected/pass"
29
+ end
30
+ end
31
+ end
32
+
33
+ describe "configure" do
34
+ it 'setup access credentials' do
35
+ expect(AwsUpload::aws_access_key_id).to eql("some_key")
36
+ expect(AwsUpload::aws_secret_access_key).to eql("some_secret")
37
+ expect(AwsUpload::aws_region).to eql("us-west-2")
38
+ end
39
+
40
+ it 'setup default uploader' do
41
+ expect(AwsUpload::uploader().bucket).to eql("default-bucket")
42
+ expect(AwsUpload::uploader().key_prefix).to eql("some-prefix/")
43
+ expect(AwsUpload::uploader().options[:acl]).to eql("private")
44
+ expect(AwsUpload::uploader().options[:content_length_range]).to eql(0..10240)
45
+ end
46
+
47
+ it 'setup named uploader' do
48
+ expect(AwsUpload::uploader(:image).bucket).to eql("image-bucket")
49
+ expect(AwsUpload::uploader(:image).key_prefix).to eql("img-")
50
+ expect(AwsUpload::uploader(:image).options[:acl]).to eql("public-read")
51
+ expect(AwsUpload::uploader(:image).options[:content_length_range]).to eql(0..1024)
52
+ expect(AwsUpload::uploader(:image).options[:success_action_redirect]).to eql("http://url.to/redirected/pass")
53
+ end
54
+ end
55
+
56
+ describe "form-helper" do
57
+ it "add aws_upload_form method to ActionView::Base" do
58
+ expect(ActionView::Base.instance_methods).to include(:aws_upload_form)
59
+ end
60
+
61
+ describe "#aws_upload_form" do
62
+ it "return generated form" do
63
+ form_html = AwsUpload::FormHelper.aws_upload_form :image, "objname"
64
+ expect(form_html).to include("<form action='https://image-bucket.s3-us-west-2.amazonaws.com' method='post' enctype='multipart/form-data'>")
65
+ expect(form_html).to be_html_safe
66
+ end
67
+
68
+ it "return allows block to specify file input and submit button" do
69
+ form_html = AwsUpload::FormHelper.aws_upload_form :image, "objname" do
70
+ "<input type='file' name='file' id='special'><input type='submit' value='GO!'>"
71
+ end
72
+ expect(form_html).to include("<input type='file' name='file' id='special'><input type='submit' value='GO!'>")
73
+ expect(form_html).to be_html_safe
74
+ end
75
+ end
76
+ end
77
+
78
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'aws_upload'
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aws_upload
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - ykmr1224
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-06 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: '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: activesupport
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: actionview
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: '1.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
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
+ description: This gem offers a helper method to build a form HTML to upload directry
98
+ to Amazon S3 storage by using POST method.
99
+ email:
100
+ - ykmr1224@gmail.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - .gitignore
106
+ - .rspec
107
+ - .travis.yml
108
+ - Gemfile
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - aws_upload.gemspec
113
+ - lib/aws_upload.rb
114
+ - lib/aws_upload/form_helper.rb
115
+ - lib/aws_upload/version.rb
116
+ - spec/aws_upload_spec.rb
117
+ - spec/spec_helper.rb
118
+ homepage: https://github.com/ykmr1224
119
+ licenses:
120
+ - MIT
121
+ metadata: {}
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - '>='
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - '>='
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubyforge_project:
138
+ rubygems_version: 2.4.5
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: This gem offers a helper method to build a form HTML to upload directry to
142
+ Amazon S3 storage by using POST method.
143
+ test_files:
144
+ - spec/aws_upload_spec.rb
145
+ - spec/spec_helper.rb