Skip to content

Instantly share code, notes, and snippets.

@joshdholtz
Created February 6, 2013 01:25
Show Gist options
  • Select an option

  • Save joshdholtz/4719396 to your computer and use it in GitHub Desktop.

Select an option

Save joshdholtz/4719396 to your computer and use it in GitHub Desktop.
Fileupload to s3
post "/" do
logger.debug "Params - " + params.to_s
photo = nil
caption = params[:caption]
params.each_value do |file|
unless file && file.is_a?(Hash) && (tmpfile = file[:tempfile]) && (name = file[:filename])
logger.debug "Cannot upload"
else
logger.debug "Can upload"
s3_file_name = SecureRandom.urlsafe_base64.to_s
while blk = tmpfile.read(65536)
AWS::S3::Base.establish_connection!(
:access_key_id => ENV['S3_KEY'],
:secret_access_key => ENV['S3_SECRET'])
AWS::S3::S3Object.store(s3_file_name, open(tmpfile),ENV['S3_BUCKET'],:access => :public_read,:content_type => 'image/png')
end
o = [('a'..'z'),('A'..'Z')].map{|i| i.to_a}.flatten
url_code = (0...12).map{ o[rand(o.length)] }.join
photo = Photo.create(:url => "https://s3.amazonaws.com/#{ENV['S3_BUCKET']}/#{s3_file_name}", :caption => caption, :url_code => url_code, :created_on => Time.now, :updated_on => Time.now)
end
end
if photo
return 200, {}, photo.json.to_json
else
return 400, {}, {}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment