Commit 05498d27 by Jon Yurek

Create a bucket if one doesn't exist.

This method prevents excessive calls to the S3 API, creating a bucket
only if the store method fails with a NoSuchBucket error instead of
asking for the bucket before each request.
parent 9e6afe4f
......@@ -133,6 +133,10 @@ module Paperclip
return file
end
def create_bucket
AWS::S3::Bucket.create(bucket_name)
end
def flush_writes #:nodoc:
@queued_for_write.each do |style, file|
begin
......@@ -143,6 +147,9 @@ module Paperclip
{:content_type => instance_read(:content_type),
:access => @s3_permissions,
}.merge(@s3_headers))
rescue AWS::S3::NoSuchBucket => e
create_bucket
retry
rescue AWS::S3::ResponseError => e
raise
end
......
......@@ -185,6 +185,21 @@ class StorageTest < Test::Unit::TestCase
end
end
context "and saved without a bucket" do
setup do
class AWS::S3::NoSuchBucket < AWS::S3::ResponseError
# Force the class to be created as a proper subclass of ResponseError thanks to AWS::S3's autocreation of exceptions
end
AWS::S3::Bucket.expects(:create).with("testing")
AWS::S3::S3Object.stubs(:store).raises(AWS::S3::NoSuchBucket.new(:message, :response)).then.returns(true)
@dummy.save
end
should "succeed" do
assert true
end
end
context "and remove" do
setup do
AWS::S3::S3Object.stubs(:exists?).returns(true)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment