Commit 044f00ec by Michał Szajbe Committed by Jon Yurek

Added support for S3 bucket being defined as a Proc and therefore determined at runtime.

(cherry picked from commit 74de215ea28628af94b2c4a0d1b005395247fdc1)
parent 87783829
......@@ -106,6 +106,8 @@ module Paperclip
# * +bucket+: This is the name of the S3 bucket that will store your files. Remember
# that the bucket must be unique across all of Amazon S3. If the bucket does not exist
# Paperclip will attempt to create it. The bucket name will not be interpolated.
# You can define the bucket as a Proc if you want to determine it's name at runtime.
# Paperclip will call that Proc with attachment as the only argument.
# * +url+: There are two options for the S3 url. You can choose to have the bucket's name
# placed domain-style (bucket.s3.amazonaws.com) or path-style (s3.amazonaws.com/bucket).
# Normally, this won't matter in the slightest and you can leave the default (which is
......@@ -122,6 +124,7 @@ module Paperclip
base.instance_eval do
@s3_credentials = parse_credentials(@options[:s3_credentials])
@bucket = @options[:bucket] || @s3_credentials[:bucket]
@bucket = @bucket.call(self) if @bucket.is_a?(Proc)
@s3_options = @options[:s3_options] || {}
@s3_permissions = @options[:s3_permissions] || 'public-read'
@s3_protocol = @options[:s3_protocol] || (@s3_permissions == 'public-read' ? 'http' : 'https')
......
......@@ -127,6 +127,19 @@ class StorageTest < Test::Unit::TestCase
end
end
context "An attachment with S3 storage and bucket defined as a Proc" do
setup do
rebuild_model :storage => :s3,
:bucket => lambda { |attachment| "bucket_#{attachment.instance.other}" },
:s3_credentials => {:not => :important}
end
should "get the right bucket name" do
assert "bucket_a", Dummy.new(:other => 'a').avatar.bucket_name
assert "bucket_b", Dummy.new(:other => 'b').avatar.bucket_name
end
end
context "An attachment with S3 storage and specific s3 headers set" do
setup do
rebuild_model :storage => :s3,
......
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