Skip to content

Instantly share code, notes, and snippets.

@kyledrake
Created January 5, 2012 18:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyledrake/1566576 to your computer and use it in GitHub Desktop.
Save kyledrake/1566576 to your computer and use it in GitHub Desktop.
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