Commit 8f1dc1e9 by Carlo Cajucom Committed by Tute Costa

Support s3 host alias with prefixes

Allows `s3_host_alias` to be set to CDNs which support S3 Bucket prefix.

[fixes #2287]
parent f8fb4a80
...@@ -65,6 +65,9 @@ module Paperclip ...@@ -65,6 +65,9 @@ module Paperclip
# * +s3_host_alias+: The fully-qualified domain name (FQDN) that is the alias to the # * +s3_host_alias+: The fully-qualified domain name (FQDN) that is the alias to the
# S3 domain of your bucket. Used with the :s3_alias_url url interpolation. See the # S3 domain of your bucket. Used with the :s3_alias_url url interpolation. See the
# link in the +url+ entry for more information about S3 domains and buckets. # link in the +url+ entry for more information about S3 domains and buckets.
# * +s3_prefixes_in_alias+: The number of prefixes that is prepended by
# s3_host_alias. This will remove the prefixes from the path in
# :s3_alias_url url interpolation
# * +url+: There are four options for the S3 url. You can choose to have the bucket's name # * +url+: There are four 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). # placed domain-style (bucket.s3.amazonaws.com) or path-style (s3.amazonaws.com/bucket).
# You can also specify a CNAME (which requires the CNAME to be specified as # You can also specify a CNAME (which requires the CNAME to be specified as
...@@ -164,7 +167,13 @@ module Paperclip ...@@ -164,7 +167,13 @@ module Paperclip
end end
Paperclip.interpolates(:s3_alias_url) do |attachment, style| Paperclip.interpolates(:s3_alias_url) do |attachment, style|
"#{attachment.s3_protocol(style, true)}//#{attachment.s3_host_alias}/#{attachment.path(style).sub(%r{\A/}, "".freeze)}" protocol = attachment.s3_protocol(style, true)
host = attachment.s3_host_alias
path = attachment.path(style).
split("/")[attachment.s3_prefixes_in_alias..-1].
join("/").
sub(%r{\A/}, "".freeze)
"#{protocol}//#{host}/#{path}"
end unless Paperclip::Interpolations.respond_to? :s3_alias_url end unless Paperclip::Interpolations.respond_to? :s3_alias_url
Paperclip.interpolates(:s3_path_url) do |attachment, style| Paperclip.interpolates(:s3_path_url) do |attachment, style|
"#{attachment.s3_protocol(style, true)}//#{attachment.s3_host_name}/#{attachment.bucket_name}/#{attachment.path(style).sub(%r{\A/}, "".freeze)}" "#{attachment.s3_protocol(style, true)}//#{attachment.s3_host_name}/#{attachment.bucket_name}/#{attachment.path(style).sub(%r{\A/}, "".freeze)}"
...@@ -213,6 +222,10 @@ module Paperclip ...@@ -213,6 +222,10 @@ module Paperclip
@s3_host_alias @s3_host_alias
end end
def s3_prefixes_in_alias
@s3_prefixes_in_alias ||= @options[:s3_prefixes_in_alias].to_i
end
def s3_url_options def s3_url_options
s3_url_options = @options[:s3_url_options] || {} s3_url_options = @options[:s3_url_options] || {}
s3_url_options = s3_url_options.call(instance) if s3_url_options.respond_to?(:call) s3_url_options = s3_url_options.call(instance) if s3_url_options.respond_to?(:call)
......
...@@ -567,6 +567,33 @@ describe Paperclip::Storage::S3 do ...@@ -567,6 +567,33 @@ describe Paperclip::Storage::S3 do
end end
end end
context "generating a url with a prefixed host alias" do
before do
rebuild_model(
aws2_add_region.merge(
storage: :s3,
s3_credentials: {
production: { bucket: "prod_bucket" },
development: { bucket: "dev_bucket" },
},
bucket: "bucket",
s3_host_alias: "something.something.com",
s3_prefixes_in_alias: 2,
path: "prefix1/prefix2/:attachment/:basename:dotextension",
url: ":s3_alias_url",
)
)
@dummy = Dummy.new
@dummy.avatar = stringy_file
@dummy.stubs(:new_record?).returns(false)
end
it "returns a url with the prefixes removed" do
assert_match %r{^//something.something.com/avatars/data[^\.]},
@dummy.avatar.url
end
end
context "generating a url with a proc as the host alias" do context "generating a url with a proc as the host alias" do
before do before do
rebuild_model (aws2_add_region).merge storage: :s3, rebuild_model (aws2_add_region).merge 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