s3-upload 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +4 -0
- data/LICENSE +20 -0
- data/README.md +32 -0
- data/Rakefile +11 -0
- data/lib/s3-upload.rb +77 -0
- data/lib/s3-upload/version.rb +3 -0
- data/test/s3_upload_test.rb +26 -0
- data/test/test_helper.rb +6 -0
- metadata +103 -0
data/CHANGELOG
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010, 2011 Niklas Holmgren, Sutajio
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
S3-Upload
|
2
|
+
=========
|
3
|
+
|
4
|
+
S3-Upload is a simple helper for creating a signed S3 upload policy, useful
|
5
|
+
for allowing untrusted 3rd parties to upload to your bucket. Through, for
|
6
|
+
example, a HTML form.
|
7
|
+
|
8
|
+
Setup
|
9
|
+
-----
|
10
|
+
|
11
|
+
$ gem install s3-upload
|
12
|
+
|
13
|
+
require 's3-upload'
|
14
|
+
S3Upload.access_key_id = ...
|
15
|
+
S3Upload.secret_access_key = ...
|
16
|
+
S3Upload.bucket = 'my-upload-bucket'
|
17
|
+
S3Upload.default_key_prefix = 'uploads/'
|
18
|
+
|
19
|
+
Example usage
|
20
|
+
-------------
|
21
|
+
|
22
|
+
upload = S3Upload.new(
|
23
|
+
:key_prefix => Time.now.to_s,
|
24
|
+
:expiration_date => 1.year.from_now # Default is 1 hour
|
25
|
+
)
|
26
|
+
render :json => upload
|
27
|
+
|
28
|
+
Author
|
29
|
+
------
|
30
|
+
|
31
|
+
Created by Niklas Holmgren (niklas@sutajio.se) and released under
|
32
|
+
the MIT license.
|
data/Rakefile
ADDED
data/lib/s3-upload.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'base64'
|
2
|
+
require 'hmac-sha1'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
require 's3-upload/version'
|
6
|
+
|
7
|
+
class S3Upload
|
8
|
+
|
9
|
+
class<<self
|
10
|
+
attr_accessor :access_key_id
|
11
|
+
attr_accessor :secret_access_key
|
12
|
+
attr_accessor :bucket
|
13
|
+
attr_accessor :default_key_prefix
|
14
|
+
end
|
15
|
+
|
16
|
+
def initialize(options = {})
|
17
|
+
@options = options
|
18
|
+
@options[:AWSAccessKeyId] ||= self.class.access_key_id
|
19
|
+
@options[:AWSSecretAccessKey] ||= self.class.secret_access_key
|
20
|
+
@options[:bucket] ||= self.class.bucket
|
21
|
+
@options[:key_prefix] ||= self.class.default_key_prefix
|
22
|
+
@options[:expiration_date] ||= Time.now + 60*60
|
23
|
+
@options[:success_action_status] = '201'
|
24
|
+
end
|
25
|
+
|
26
|
+
def url
|
27
|
+
"http://#{@options[:bucket]}.s3.amazonaws.com/"
|
28
|
+
end
|
29
|
+
|
30
|
+
def file_param
|
31
|
+
'file'
|
32
|
+
end
|
33
|
+
|
34
|
+
def policy
|
35
|
+
conditions = []
|
36
|
+
conditions << { :bucket => @options[:bucket].to_s }
|
37
|
+
conditions << ['starts-with', '$key', @options[:key_prefix].to_s]
|
38
|
+
conditions << { :success_action_redirect => @options[:success_action_redirect].to_s } if @options[:success_action_redirect]
|
39
|
+
conditions << { :success_action_status => @options[:success_action_status].to_s } if @options[:success_action_status]
|
40
|
+
conditions << ['starts-with', '$Filename', '']
|
41
|
+
if @options[:meta_fields]
|
42
|
+
@options[:meta_fields].sort.each do |meta_field|
|
43
|
+
conditions << ['starts-with', "$#{meta_field}", '']
|
44
|
+
end
|
45
|
+
end
|
46
|
+
Base64.encode64(
|
47
|
+
"{
|
48
|
+
'expiration': '#{self.class.expiration_date(@options[:expiration_date])}',
|
49
|
+
'conditions': #{conditions.to_json}
|
50
|
+
}").gsub(/\n|\r/, '')
|
51
|
+
end
|
52
|
+
|
53
|
+
def signature
|
54
|
+
Base64.encode64(HMAC::SHA1.digest(@options[:AWSSecretAccessKey],policy)).strip
|
55
|
+
end
|
56
|
+
|
57
|
+
def params
|
58
|
+
{ :success_action_status => @options[:success_action_status],
|
59
|
+
:AWSAccessKeyId => @options[:AWSAccessKeyId],
|
60
|
+
:key => "#{@options[:key_prefix]}${filename}",
|
61
|
+
:policy => self.policy,
|
62
|
+
:signature => self.signature }
|
63
|
+
end
|
64
|
+
|
65
|
+
def as_json(options = {})
|
66
|
+
{ :url => self.url,
|
67
|
+
:file_param => self.file_param,
|
68
|
+
:params => self.params }
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
|
73
|
+
def self.expiration_date(seconds_from_now)
|
74
|
+
seconds_from_now.utc.strftime('%Y-%m-%dT%H:%M:%S.000Z')
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require File.expand_path('test/test_helper')
|
2
|
+
|
3
|
+
class S3UploadTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
S3Upload.access_key_id = '...'
|
7
|
+
S3Upload.secret_access_key = '...'
|
8
|
+
S3Upload.bucket = 'my-upload-bucket'
|
9
|
+
S3Upload.default_key_prefix = 'uploads/'
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_can_create_an_upload
|
13
|
+
upload = S3Upload.new(
|
14
|
+
:key_prefix => 'test/',
|
15
|
+
:expiration_date => Time.now + 60
|
16
|
+
)
|
17
|
+
assert_equal 'http://my-upload-bucket.s3.amazonaws.com/', upload.as_json[:url]
|
18
|
+
assert_equal 'file', upload.as_json[:file_param]
|
19
|
+
assert_equal '...', upload.as_json[:params][:AWSAccessKeyId]
|
20
|
+
assert_match /^test\//, upload.as_json[:params][:key]
|
21
|
+
assert_equal '201', upload.as_json[:params][:success_action_status]
|
22
|
+
assert upload.as_json[:params][:policy]
|
23
|
+
assert upload.as_json[:params][:signature]
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: s3-upload
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
version: "0.1"
|
9
|
+
platform: ruby
|
10
|
+
authors:
|
11
|
+
- Niklas Holmgren
|
12
|
+
autorequire:
|
13
|
+
bindir: bin
|
14
|
+
cert_chain: []
|
15
|
+
|
16
|
+
date: 2011-09-10 00:00:00 +02:00
|
17
|
+
default_executable:
|
18
|
+
dependencies:
|
19
|
+
- !ruby/object:Gem::Dependency
|
20
|
+
name: json
|
21
|
+
prerelease: false
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ">"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 1
|
29
|
+
- 0
|
30
|
+
- 0
|
31
|
+
version: 1.0.0
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: ruby-hmac
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ~>
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
segments:
|
43
|
+
- 0
|
44
|
+
- 4
|
45
|
+
- 0
|
46
|
+
version: 0.4.0
|
47
|
+
type: :runtime
|
48
|
+
version_requirements: *id002
|
49
|
+
description: |-
|
50
|
+
S3-Upload is a simple helper for creating a signed S3 upload policy, useful
|
51
|
+
for allowing untrusted 3rd parties to upload to your bucket. Through, for
|
52
|
+
example, a HTML form.
|
53
|
+
email: niklas@sutajio.se
|
54
|
+
executables: []
|
55
|
+
|
56
|
+
extensions: []
|
57
|
+
|
58
|
+
extra_rdoc_files:
|
59
|
+
- LICENSE
|
60
|
+
- README.md
|
61
|
+
files:
|
62
|
+
- README.md
|
63
|
+
- Rakefile
|
64
|
+
- LICENSE
|
65
|
+
- CHANGELOG
|
66
|
+
- lib/s3-upload/version.rb
|
67
|
+
- lib/s3-upload.rb
|
68
|
+
- test/s3_upload_test.rb
|
69
|
+
- test/test_helper.rb
|
70
|
+
has_rdoc: true
|
71
|
+
homepage: http://github.com/sutajio/s3-upload/
|
72
|
+
licenses: []
|
73
|
+
|
74
|
+
post_install_message:
|
75
|
+
rdoc_options:
|
76
|
+
- --charset=UTF-8
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
segments:
|
85
|
+
- 0
|
86
|
+
version: "0"
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
segments:
|
93
|
+
- 0
|
94
|
+
version: "0"
|
95
|
+
requirements: []
|
96
|
+
|
97
|
+
rubyforge_project:
|
98
|
+
rubygems_version: 1.3.7
|
99
|
+
signing_key:
|
100
|
+
specification_version: 3
|
101
|
+
summary: S3-Upload is a simple helper for creating a signed S3 upload policy
|
102
|
+
test_files: []
|
103
|
+
|