forked from carsonmcdonald/direct-browser-s3-upload-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
36 lines (27 loc) · 822 Bytes
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
require 'sinatra'
require 'base64'
require 'openssl'
require 'cgi'
S3_KEY='S3 key here'
S3_SECRET='S3 secret here'
S3_BUCKET='/uploadtestbucket'
EXPIRE_TIME=(60 * 5) # 5 minutes
S3_URL='http://s3.amazonaws.com'
get '/' do
send_file 'index.html'
end
get '/styles.css' do
send_file 'styles.css'
end
get '/app.js' do
send_file 'app.js'
end
get '/signput' do
objectName = "/#{params['name']}"
mimeType = params['type']
expires = Time.now.to_i + EXPIRE_TIME
amzHeaders = "x-amz-acl:public-read"
stringToSign = "PUT\n\n#{mimeType}\n#{expires}\n#{amzHeaders}\n#{S3_BUCKET}#{objectName}";
sig = CGI::escape(Base64.strict_encode64(OpenSSL::HMAC.digest('sha1', S3_SECRET, stringToSign)))
CGI::escape("#{S3_URL}#{S3_BUCKET}#{objectName}?AWSAccessKeyId=#{S3_KEY}&Expires=#{expires}&Signature=#{sig}")
end