Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
Support s3 host alias with prefixes
Browse files Browse the repository at this point in the history
Allows `s3_host_alias` to be set to CDNs which support S3 Bucket prefix.

[fixes #2287]
  • Loading branch information
cjcaj authored and tute committed Aug 30, 2016
1 parent f8fb4a8 commit 8f1dc1e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/paperclip/storage/s3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ module Storage
# * +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
# 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
# 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
Expand Down Expand Up @@ -164,7 +167,13 @@ def self.extended base
end

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
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)}"
Expand Down Expand Up @@ -213,6 +222,10 @@ def s3_host_alias
@s3_host_alias
end

def s3_prefixes_in_alias
@s3_prefixes_in_alias ||= @options[:s3_prefixes_in_alias].to_i
end

def 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)
Expand Down
27 changes: 27 additions & 0 deletions spec/paperclip/storage/s3_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,33 @@ def original_filename
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
before do
rebuild_model (aws2_add_region).merge storage: :s3,
Expand Down

0 comments on commit 8f1dc1e

Please sign in to comment.