Skip to content

Instantly share code, notes, and snippets.

@daz4126
Created August 17, 2010 18:52
Show Gist options
  • Save daz4126/531427 to your computer and use it in GitHub Desktop.
Save daz4126/531427 to your computer and use it in GitHub Desktop.
require 'sinatra'
require 'aws/s3'
get '/' do
haml :upload
end
post '/' do
unless params[:file] && (tmpfile = params[:file][:tempfile]) && (name = params[:file][:filename])
return haml(:upload)
end
while blk = tmpfile.read(65536)
if settings.environment == :production
AWS::S3::Base.establish_connection!(
:access_key_id => ENV['S3_KEY'],
:secret_access_key => ENV['S3_SECRET']
)
AWS::S3::S3Object.store(name,open(tmpfile),ENV['S3_BUCKET'],:access => :public_read)
else
File.open(File.join(Dir.pwd,"public/images", name), "wb") { |f| f.write(tmpfile.read) }
end
end
redirect '/'
end
__END__
@@ upload
%h2 Upload
%form{:action=>"/upload",:method=>"post",:enctype=>"multipart/form-data"}
%input{:type=>"file",:name=>"file"}
%input{:type=>"submit",:value=>"Upload"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment