Created
May 29, 2012 15:09
-
-
Save dansimco/2828955 to your computer and use it in GitHub Desktop.
Concise Sinatra Amazon S3 Upload Example
This file contains hidden or 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
works like a charm!