Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fix for AWS::S3 thread safety issues
require 'aws/s3'
class D7Obj < AWS::S3::S3Object
set_current_bucket_to "kd-test"
end
class S3Handler
AWS::S3::Base.establish_connection!(
:access_key_id => 'ID',
:secret_access_key => 'SECRET',
:persistent => true
)
def connected
if AWS::S3::Base.connected? == true
return true
else
return false
end
end
def save_file(file, filename, access_level = "public-read")
return D7Obj.store(filename, file, :access => :public_read) unless connected == false
end
end
s3 = S3Handler.new
semaphore = Mutex.new
threads = []
5.times { |i|
threads << Thread.new(i) {
semaphore.synchronize do
s3 = S3Handler.new
s3.save_file(open('./Downloads/test.jpg'), "#{i}.jpg")
puts("finished saving file #{i}.jpg")
end
}
}
threads.each {|t| t.join }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment