From 6498f448bf0fac80006da194f00d83926083fae7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marjan=20Krekoten=27=20=28=D0=9C=D0=B0=D1=80=27=D1=8F?= =?UTF-8?q?=D0=BD=20=D0=9A=D1=80=D0=B5=D0=BA=D0=BE=D1=82=D0=B5=D0=BD=D1=8C?= =?UTF-8?q?=29?= Date: Tue, 31 Jan 2017 17:01:42 +0200 Subject: [PATCH] Added fog_aws_accelerate options for fog storage provider AWS S3 supports accelerated file transfers to and from S3. [Transfer Acceleration](http://amzn.to/2jQmpAX) This introduces ability to configure Carrierwave to return accelerated public urls since Carrierwave constructs them by itself for speed concerns. To enable this feauture you need to set fog_aws_accelerate to true. It is false by default. Changes added: * fog_aws_accelerate added to fog aws configuration options * corresponding specs updated * corresponding docs updated --- lib/carrierwave/storage/fog.rb | 5 ++++- lib/carrierwave/uploader/configuration.rb | 3 ++- spec/storage/fog_helper.rb | 18 +++++++++++++++--- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/carrierwave/storage/fog.rb b/lib/carrierwave/storage/fog.rb index ebdf4e2fa..c961874af 100644 --- a/lib/carrierwave/storage/fog.rb +++ b/lib/carrierwave/storage/fog.rb @@ -16,6 +16,8 @@ module Storage # [:fog_authenticated_url_expiration] (optional) time (in seconds) that authenticated urls # will be valid, when fog_public is false and provider is AWS or Google, defaults to 600 # [:fog_use_ssl_for_aws] (optional) #public_url will use https for the AWS generated URL] + # [:fog_aws_accelerate] (optional) #public_url will use s3-accelerate subdomain + # instead of s3, defaults to false # # # AWS credentials contain the following keys: @@ -349,7 +351,8 @@ def public_url protocol = @uploader.fog_use_ssl_for_aws ? "https" : "http" # if directory is a valid subdomain, use that style for access if @uploader.fog_directory.to_s =~ /^(?:[a-z]|\d(?!\d{0,2}(?:\d{1,3}){3}$))(?:[a-z0-9\.]|(?![\-])|\-(?![\.])){1,61}[a-z0-9]$/ - "#{protocol}://#{@uploader.fog_directory}.s3.amazonaws.com/#{encoded_path}" + s3_subdomain = @uploader.fog_aws_accelerate ? "s3-accelerate" : "s3" + "#{protocol}://#{@uploader.fog_directory}.#{s3_subdomain}.amazonaws.com/#{encoded_path}" else # directory is not a valid subdomain, so use path style for access "#{protocol}://s3.amazonaws.com/#{@uploader.fog_directory}/#{encoded_path}" diff --git a/lib/carrierwave/uploader/configuration.rb b/lib/carrierwave/uploader/configuration.rb index 453bf83b9..be79e440a 100644 --- a/lib/carrierwave/uploader/configuration.rb +++ b/lib/carrierwave/uploader/configuration.rb @@ -30,6 +30,7 @@ module Configuration add_config :fog_public add_config :fog_authenticated_url_expiration add_config :fog_use_ssl_for_aws + add_config :fog_aws_accelerate # Mounting add_config :ignore_integrity_errors @@ -177,6 +178,7 @@ def reset_config config.fog_public = true config.fog_authenticated_url_expiration = 600 config.fog_use_ssl_for_aws = true + config.fog_aws_accelerate = false config.store_dir = 'uploads' config.cache_dir = 'uploads/tmp' config.delete_tmp_file_after_storage = true @@ -200,4 +202,3 @@ def reset_config end end end - diff --git a/spec/storage/fog_helper.rb b/spec/storage/fog_helper.rb index c87068a80..cce4d4c5a 100644 --- a/spec/storage/fog_helper.rb +++ b/spec/storage/fog_helper.rb @@ -85,10 +85,22 @@ class FogSpec#{fog_credentials[:provider]}Uploader < CarrierWave::Uploader::Base end end - it "should use a subdomain URL for AWS if the directory is a valid subdomain" do - if @provider == 'AWS' + context "directory is a valid subdomain" do + before do allow(@uploader).to receive(:fog_directory).and_return('assets.site.com') - expect(@fog_file.public_url).to include('https://assets.site.com.s3.amazonaws.com') + end + + it "should use a subdomain URL for AWS" do + if @provider == 'AWS' + expect(@fog_file.public_url).to include('https://assets.site.com.s3.amazonaws.com') + end + end + + it "should use accelerate domain if fog_aws_accelerate is true" do + if @provider == 'AWS' + allow(@uploader).to receive(:fog_aws_accelerate).and_return(true) + expect(@fog_file.public_url).to include('https://assets.site.com.s3-accelerate.amazonaws.com') + end end end