Skip to content

Instantly share code, notes, and snippets.

@kiennt
Last active December 11, 2015 09:08
Show Gist options
  • Save kiennt/4577737 to your computer and use it in GitHub Desktop.
Save kiennt/4577737 to your computer and use it in GitHub Desktop.
Upload to S3 asynchronously with eventmachine and happening library
require 'securerandom'
require 'eventmachine'
require 'happening'
EM.run do
count = 10
pending = count
count.times do |index|
on_error = Proc.new do |http|
puts "An error occured: #{http.response_header.status}"
EM.stop
end
key = "#{SecureRandom.uuid}.jpg"
item = Happening::S3::Item.new(ENV['AWS_S3_BUCKET_NAME'], key,
:aws_access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:aws_secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'],
:permissions => 'public-read')
filepath = '/DEVELOPMENT/ODESK/simple_prints/SP/server/StoryTree/tests/fixtures/image7.jpg'
item.put(File.read(filepath), :on_error => on_error,
:headers => { "Content-Type" => "image/jpeg" }) do |response|
puts "Upload #{index} finish #{item.url}"
pending -= 1
EM.stop if pending == 0
end
end
end
@kiennt
Copy link
Author

kiennt commented Jan 20, 2013

I am considering to use 'em-synchrony' to avoid callback hell.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment