-
-
Save heyalexej/8794341 to your computer and use it in GitHub Desktop.
Concise Sinatra Amazon S3 Upload Example (w/o Cloudfront)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "rubygems" | |
require 'sinatra' | |
require "aws/s3" | |
get '/' do | |
return %Q{ | |
<form action="upload" method="post" accept-charset="utf-8" enctype="multipart/form-data"> | |
<div> | |
<input type="file" name="file" value="" id="file"> | |
</div> | |
<div> | |
<input type="submit" value="Upload ↑"> | |
</div> | |
</form> | |
} | |
end | |
post '/upload' do | |
awskey = 'mykey' | |
awssecret = 'mysecret' | |
bucket = 'mybucket' | |
file = params[:file][:tempfile] | |
filename = params[:file][:filename] | |
AWS::S3::Base.establish_connection!( | |
:access_key_id => awskey, | |
:secret_access_key => awssecret | |
) | |
AWS::S3::S3Object.store( | |
filename, | |
open(file.path), | |
bucket, | |
:access => :public_read | |
) | |
url = "https://#{bucket}.s3.amazonaws.com/#{filename}" | |
return url | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment