d2s3 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +20 -0
- data/README.markdown +75 -0
- data/Rakefile +13 -0
- data/d2s3.gemspec +19 -0
- data/init.rb +2 -0
- data/lib/d2s3.rb +2 -0
- data/lib/d2s3/s3_config.rb +34 -0
- data/lib/d2s3/version.rb +3 -0
- data/lib/d2s3/view_helpers.rb +54 -0
- data/tasks/d2s3_tasks.rake +4 -0
- data/test/d2s3_test.rb +8 -0
- data/test/test_helper.rb +3 -0
- metadata +76 -0
data/.gitignore
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# Compiled source #
|
2
|
+
###################
|
3
|
+
*.com
|
4
|
+
*.class
|
5
|
+
*.dll
|
6
|
+
*.exe
|
7
|
+
*.o
|
8
|
+
*.so
|
9
|
+
*.pyc
|
10
|
+
|
11
|
+
# Logs and databases #
|
12
|
+
######################
|
13
|
+
*.log
|
14
|
+
|
15
|
+
# OS generated files #
|
16
|
+
######################
|
17
|
+
.DS_Store*
|
18
|
+
ehthumbs.db
|
19
|
+
Icon?
|
20
|
+
Thumbs.db
|
data/README.markdown
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# **d2s3 (Direct to S3)**
|
2
|
+
The d2s3 (direct to s3) gem adds is a simple Ruby on Rails helper that generates an upload form that will take a given file and upload it directly to your S3 bucket, bypassing your server. This has various benefits, the biggest being that a large upload will not tie up your server from serving other requests. This gem is based on the instructions from the following Amazon tutorial: [http://developer.amazonwebservices.com/connect/entry.jspa?externalID=1434](http://developer.amazonwebservices.com/connect/entry.jspa?externalID=1434/ "Browser Uploads to S3 using HTML POST Forms")
|
3
|
+
|
4
|
+
### Why?
|
5
|
+
This was built as a solution to a problem we had where images being uploaded to be processed by [Paperclip](http://thoughtbot.com/projects/paperclip "Thoughtbot - Paperclip") were consuming [Thin](http://code.macournoyer.com/thin/ "Thin - Another Web Server") servers so they were unable to process other requests.
|
6
|
+
|
7
|
+
#### Install:
|
8
|
+
|
9
|
+
gem install d2s3
|
10
|
+
|
11
|
+
#### An example workflow using d2s3
|
12
|
+
1. Upload file with the `s3_http_upload_tag` helper form tag
|
13
|
+
2. With the data returned from the GET HTTP request from Amazon, create a message using Amazon's SQS service with the appropriate information needed to later process the image with [Paperclip](http://thoughtbot.com/projects/paperclip "Thoughtbot - Paperclip")
|
14
|
+
3. Create an API end point for a background process to access that accepts information to process the image with Paperclip. In our case, it accepts the ID of a photo album and the path to the photo to be processed
|
15
|
+
4. Running a back end process that monitors the SQS queue for new images and processes them immediately
|
16
|
+
|
17
|
+
We don't have immediate processing of our images with this workflow, but it's very quick assuming how large the queue is and how many back end processes we have actually running through the queue.
|
18
|
+
|
19
|
+
### Example d2s3 usage
|
20
|
+
<%= s3_http_upload_tag :key => 'uploads',
|
21
|
+
:content_type => 'image/jpeg',
|
22
|
+
:redirect => image_processing_url,
|
23
|
+
:acl => 'public-read',
|
24
|
+
:max_filesize => 5.megabytes,
|
25
|
+
:submit_button => '<input type="submit" value="Upload" class="button" id="upload-button">',
|
26
|
+
:form => {:style => 'display: inline;'} %>
|
27
|
+
|
28
|
+
The above helper will generate the following similar HTML form, generating all of the appropriate field keys, policy, and signature based on your Amazon Web Services YAML configuration file. The form parameter also accepts a class and id for further customization.
|
29
|
+
|
30
|
+
<form action="https://YOUR_S3_BUCKET.s3.amazonaws.com/" method="post" enctype="multipart/form-data" style="display: inline;">
|
31
|
+
<input type="hidden" name="key" value="uploads/${filename}">
|
32
|
+
<input type="hidden" name="AWSAccessKeyId" value="YOUR_AWS_ACCESS_KEY">
|
33
|
+
<input type="hidden" name="acl" value="public-read">
|
34
|
+
<input type="hidden" name="success_action_redirect" value="/image_processing_url">
|
35
|
+
<input type="hidden" name="policy" value="YOUR_POLICY_DOCUMENT_BASE64_ENCODED">
|
36
|
+
<input type="hidden" name="signature" value="YOUR_CALCULATED_SIGNATURE">
|
37
|
+
<input type="hidden" name="Content-Type" value="image/jpeg">
|
38
|
+
<input name="file" type="file"><input type="submit" value="Upload" class="button" id="upload-button">
|
39
|
+
</form>
|
40
|
+
|
41
|
+
### Return HTTP GET request from Amazon made to the redirect you declared
|
42
|
+
Parameters: {"bucket"=>"BUCKET_NAME",
|
43
|
+
"etag"=>"ETAG_HASH",
|
44
|
+
"action"=>"YOUR_REDIRECT_URL",
|
45
|
+
"controller"=>"CONTROLLER",
|
46
|
+
"key"=>"PATH/FILENAME.EXTENSION"}
|
47
|
+
|
48
|
+
### Options:
|
49
|
+
* **:content_type**
|
50
|
+
* Accepts a standard content type, otherwise it will default to binary/octet-stream
|
51
|
+
* **:redirect**
|
52
|
+
* Directs the form where the GET request from Amazon should be made once the HTTP POST is successful
|
53
|
+
* **:success\_action\_status**
|
54
|
+
* The status code returned to the client upon successful upload if success_action_redirect is not specified. Accepts 200, 201 and 204. Defaults to 204
|
55
|
+
* **:acl**
|
56
|
+
* Accepts either 'public-read' or 'private'. If blank, it defaults to 'public-read'
|
57
|
+
* **:expiration_date**
|
58
|
+
* Accepts time in the form of "3.hours" or "25.minutes". If blank, it defaults to a 10 hour window before the policy on the upload expires
|
59
|
+
* **:max_filesize**
|
60
|
+
* Accepts a max file size in the format of "5.megabytes". If blank, it defaults to 1.megabyte
|
61
|
+
* **:submit_button**
|
62
|
+
* Accepts any text to represent the submit button for the form. This allows for a very custom submit button. If blank, it defaults to `<input type="submit" value="Upload">`
|
63
|
+
* **:form => {:id => '', :class => '', :style => ''}**
|
64
|
+
* Accepts a hash of :class, :id, and :style to add customization to the form as a whole
|
65
|
+
|
66
|
+
|
67
|
+
### **TODO**
|
68
|
+
* Test in Rails 2
|
69
|
+
* Add travis
|
70
|
+
|
71
|
+
### **Contributors**
|
72
|
+
* _**Matthew Williams**_
|
73
|
+
* _**Gabriel Cebrian**_
|
74
|
+
|
75
|
+
Thanks to the [s3-swf-upload](http://github.com/elcgit/s3-swf-upload-plugin/tree/master "s3-swf-upload GitHub Project Page") plugin which code was borrowed from to make this project happen.
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
|
4
|
+
desc 'Default: run unit tests.'
|
5
|
+
task :default => :test
|
6
|
+
|
7
|
+
desc 'Test the d2s3 plugin.'
|
8
|
+
Rake::TestTask.new(:test) do |t|
|
9
|
+
t.libs << 'lib'
|
10
|
+
t.libs << 'test'
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
data/d2s3.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/d2s3/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Matthew Williams"]
|
6
|
+
gem.email = ["matthew.d.williams@gmail.com"]
|
7
|
+
gem.description = %q{d2s3 (direct to s3) is a simple Ruby on Rails helper that generates an upload form that will take a given file and upload it directly to your S3 bucket, bypassing your server}
|
8
|
+
gem.summary = %q{Ruby on Rails helper to generate S3 upload form}
|
9
|
+
gem.homepage = "https://github.com/mwilliams/d2s3"
|
10
|
+
gem.license = ""
|
11
|
+
|
12
|
+
gem.files = `git ls-files`.split("\n")
|
13
|
+
gem.test_files = `git ls-files -- {spec}/*`.split("\n")
|
14
|
+
gem.name = "d2s3"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = D2S3::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency 'rake'
|
19
|
+
end
|
data/init.rb
ADDED
data/lib/d2s3.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
module D2S3
|
2
|
+
if defined?(Rails::Railtie)
|
3
|
+
class S3Config < Rails::Railtie
|
4
|
+
require 'yaml'
|
5
|
+
|
6
|
+
cattr_reader :access_key_id, :secret_access_key, :bucket
|
7
|
+
|
8
|
+
initializer :load_s3_config, :after => :load_environment_config, :group => :all do
|
9
|
+
require File.join(Rails.root, "config", "environment.rb")
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.load_config
|
13
|
+
filename = Rails.root.join("config", "amazon_s3.yml").to_s
|
14
|
+
config = YAML.load_file(filename)
|
15
|
+
|
16
|
+
unless config
|
17
|
+
raise "Config object from #{filename} is nil"
|
18
|
+
end
|
19
|
+
|
20
|
+
unless config[Rails.env]
|
21
|
+
raise "No environment #{Rails.env} found in #{filename}"
|
22
|
+
end
|
23
|
+
|
24
|
+
@@access_key_id = config[Rails.env]['access_key_id']
|
25
|
+
@@secret_access_key = config[Rails.env]['secret_access_key']
|
26
|
+
@@bucket = config[Rails.env]['bucket_name']
|
27
|
+
|
28
|
+
unless @@access_key_id && @@secret_access_key && @@bucket
|
29
|
+
raise "Please configure your S3 settings in #{filename}."
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/lib/d2s3/version.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'base64'
|
2
|
+
require 'openssl'
|
3
|
+
require 'digest/sha1'
|
4
|
+
|
5
|
+
module D2S3
|
6
|
+
module ViewHelpers
|
7
|
+
|
8
|
+
def s3_http_upload_tag(options = {})
|
9
|
+
bucket = options[:bucket] || D2S3::S3Config.bucket
|
10
|
+
access_key_id = options[:access_key_id] || D2S3::S3Config.access_key_id
|
11
|
+
secret_access_key = options[:secret_access_key] || D2S3::S3Config.secret_access_key
|
12
|
+
key = options[:key] || ''
|
13
|
+
content_type = options[:content_type] || '' # Defaults to binary/octet-stream if blank
|
14
|
+
redirect = options[:redirect] || '/'
|
15
|
+
success_action_status = options[:success_action_status] || 204
|
16
|
+
acl = options[:acl] || 'public-read'
|
17
|
+
expiration_date = (options[:expiration_date] || 10.hours).from_now.utc.strftime('%Y-%m-%dT%H:%M:%S.000Z')
|
18
|
+
max_filesize = options[:max_filesize] || 1.megabyte
|
19
|
+
submit_button = options[:submit_button] || '<input type="submit" value="Upload">'
|
20
|
+
|
21
|
+
options[:form] ||= {}
|
22
|
+
options[:form][:id] ||= 'upload-form'
|
23
|
+
options[:form][:class] ||= 'upload-form'
|
24
|
+
|
25
|
+
policy = Base64.encode64(
|
26
|
+
"{'expiration': '#{expiration_date}',
|
27
|
+
'conditions': [
|
28
|
+
{'bucket': '#{bucket}'},
|
29
|
+
['starts-with', '$key', '#{key}'],
|
30
|
+
{'acl': '#{acl}'},
|
31
|
+
{'success_action_redirect': '#{redirect}'},
|
32
|
+
{'success_action_status': '#{success_action_status}'},
|
33
|
+
['starts-with', '#{content_type}', ''],
|
34
|
+
['content-length-range', 0, #{max_filesize}]
|
35
|
+
]
|
36
|
+
}").gsub("\n", "")
|
37
|
+
|
38
|
+
signature = Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha1'), secret_access_key, policy)).gsub(/\n/, '')
|
39
|
+
|
40
|
+
%(<form action="https://#{bucket}.s3.amazonaws.com/" method="post" enctype="multipart/form-data" id="#{options[:form][:id]}" class="#{options[:form][:class]}" style="#{options[:form][:style]}">
|
41
|
+
<input type="hidden" name="key" value="#{key}/${filename}">
|
42
|
+
<input type="hidden" name="AWSAccessKeyId" value="#{access_key_id}">
|
43
|
+
<input type="hidden" name="acl" value="#{acl}">
|
44
|
+
<input type="hidden" name="success_action_redirect" value="#{redirect}">
|
45
|
+
<input type="hidden" name="success_action_status" value="#{success_action_status}">
|
46
|
+
<input type="hidden" name="policy" value="#{policy}">
|
47
|
+
<input type="hidden" name="signature" value="#{signature}">
|
48
|
+
<input name="file" type="file">#{submit_button}
|
49
|
+
</form>).html_safe
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
ActionView::Base.send(:include, D2S3::ViewHelpers)
|
data/test/d2s3_test.rb
ADDED
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: d2s3
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Matthew Williams
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-07 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: d2s3 (direct to s3) is a simple Ruby on Rails helper that generates an
|
31
|
+
upload form that will take a given file and upload it directly to your S3 bucket,
|
32
|
+
bypassing your server
|
33
|
+
email:
|
34
|
+
- matthew.d.williams@gmail.com
|
35
|
+
executables: []
|
36
|
+
extensions: []
|
37
|
+
extra_rdoc_files: []
|
38
|
+
files:
|
39
|
+
- .gitignore
|
40
|
+
- README.markdown
|
41
|
+
- Rakefile
|
42
|
+
- d2s3.gemspec
|
43
|
+
- init.rb
|
44
|
+
- lib/d2s3.rb
|
45
|
+
- lib/d2s3/s3_config.rb
|
46
|
+
- lib/d2s3/version.rb
|
47
|
+
- lib/d2s3/view_helpers.rb
|
48
|
+
- tasks/d2s3_tasks.rake
|
49
|
+
- test/d2s3_test.rb
|
50
|
+
- test/test_helper.rb
|
51
|
+
homepage: https://github.com/mwilliams/d2s3
|
52
|
+
licenses:
|
53
|
+
- ''
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ! '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
requirements: []
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 1.8.23
|
73
|
+
signing_key:
|
74
|
+
specification_version: 3
|
75
|
+
summary: Ruby on Rails helper to generate S3 upload form
|
76
|
+
test_files: []
|