Last active
December 11, 2015 09:08
-
-
Save kiennt/4577737 to your computer and use it in GitHub Desktop.
Upload to S3 asynchronously with eventmachine and happening library
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 '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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am considering to use 'em-synchrony' to avoid callback hell.