From 682175c6bcbe7fac2f8621f1da6fa8e748a84ecb Mon Sep 17 00:00:00 2001 From: "Mohammed O. Tillawy" Date: Thu, 14 Sep 2023 11:19:39 +0300 Subject: [PATCH 1/3] support virtual_host in presigned_url when using s3_host_alias when using options: { url: "s3_alias_url" & "s3_host_alias": "bucket_alias.domain.com" }, make the presigned_url : https://bucket_alias.domain.com/file?X-Amz-Credential= instead of: https://s3.amazonaws.com/bucket_alias.domain.com/file?X-Amz-Credential= --- lib/paperclip/storage/s3.rb | 5 ++++- spec/paperclip/storage/s3_spec.rb | 8 +++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/paperclip/storage/s3.rb b/lib/paperclip/storage/s3.rb index 70cf5fc70..70273e27e 100644 --- a/lib/paperclip/storage/s3.rb +++ b/lib/paperclip/storage/s3.rb @@ -187,7 +187,10 @@ def self.extended(base) def expiring_url(time = 3600, style_name = default_style) if path(style_name) - base_options = { expires_in: time } + base_options = { + expires_in: time, + virtual_host: @options[:url] == ":s3_alias_url" && @options[:s3_host_alias] != nil + } s3_object(style_name).presigned_url( :get, base_options.merge(s3_url_options) diff --git a/spec/paperclip/storage/s3_spec.rb b/spec/paperclip/storage/s3_spec.rb index f132d2121..2da845175 100644 --- a/spec/paperclip/storage/s3_spec.rb +++ b/spec/paperclip/storage/s3_spec.rb @@ -741,7 +741,7 @@ def counter object = double allow(@dummy.avatar).to receive(:s3_object).and_return(object) - expect(object).to receive(:presigned_url).with(:get, { expires_in: 3600 }) + expect(object).to receive(:presigned_url).with(:get, { expires_in: 3600, virtual_host: true }) @dummy.avatar.expiring_url end end @@ -761,6 +761,7 @@ def counter { expires_in: 3600, response_content_disposition: "inline", + virtual_host: true }, ) @dummy.avatar.expiring_url @@ -789,6 +790,7 @@ def counter { expires_in: 3600, response_content_type: "image/png", + virtual_host: true }, ) @dummy.avatar.expiring_url @@ -837,14 +839,14 @@ def counter it "generates a url for the thumb" do object = double allow(@dummy.avatar).to receive(:s3_object).with(:thumb).and_return(object) - expect(object).to receive(:presigned_url).with(:get, { expires_in: 1800 }) + expect(object).to receive(:presigned_url).with(:get, { expires_in: 1800, virtual_host: true }) @dummy.avatar.expiring_url(1800, :thumb) end it "generates a url for the default style" do object = double allow(@dummy.avatar).to receive(:s3_object).with(:original).and_return(object) - expect(object).to receive(:presigned_url).with(:get, { expires_in: 1800 }) + expect(object).to receive(:presigned_url).with(:get, { expires_in: 1800, virtual_host: true }) @dummy.avatar.expiring_url(1800) end end From 89c59c1541a33667197ae9ca6256c5fa1cd63a86 Mon Sep 17 00:00:00 2001 From: "Mohammed O. Tillawy" Date: Thu, 14 Sep 2023 11:19:39 +0300 Subject: [PATCH 2/3] support virtual_host in presigned_url when using s3_host_alias when using options: { url: "s3_alias_url" & "s3_host_alias": "bucket_alias.domain.com" }, make the presigned_url : https://bucket_alias.domain.com/file?X-Amz-Credential= instead of: https://s3.amazonaws.com/bucket_alias.domain.com/file?X-Amz-Credential= --- lib/paperclip/storage/s3.rb | 5 ++++- spec/paperclip/storage/s3_spec.rb | 8 +++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/paperclip/storage/s3.rb b/lib/paperclip/storage/s3.rb index 70cf5fc70..70273e27e 100644 --- a/lib/paperclip/storage/s3.rb +++ b/lib/paperclip/storage/s3.rb @@ -187,7 +187,10 @@ def self.extended(base) def expiring_url(time = 3600, style_name = default_style) if path(style_name) - base_options = { expires_in: time } + base_options = { + expires_in: time, + virtual_host: @options[:url] == ":s3_alias_url" && @options[:s3_host_alias] != nil + } s3_object(style_name).presigned_url( :get, base_options.merge(s3_url_options) diff --git a/spec/paperclip/storage/s3_spec.rb b/spec/paperclip/storage/s3_spec.rb index f132d2121..2da845175 100644 --- a/spec/paperclip/storage/s3_spec.rb +++ b/spec/paperclip/storage/s3_spec.rb @@ -741,7 +741,7 @@ def counter object = double allow(@dummy.avatar).to receive(:s3_object).and_return(object) - expect(object).to receive(:presigned_url).with(:get, { expires_in: 3600 }) + expect(object).to receive(:presigned_url).with(:get, { expires_in: 3600, virtual_host: true }) @dummy.avatar.expiring_url end end @@ -761,6 +761,7 @@ def counter { expires_in: 3600, response_content_disposition: "inline", + virtual_host: true }, ) @dummy.avatar.expiring_url @@ -789,6 +790,7 @@ def counter { expires_in: 3600, response_content_type: "image/png", + virtual_host: true }, ) @dummy.avatar.expiring_url @@ -837,14 +839,14 @@ def counter it "generates a url for the thumb" do object = double allow(@dummy.avatar).to receive(:s3_object).with(:thumb).and_return(object) - expect(object).to receive(:presigned_url).with(:get, { expires_in: 1800 }) + expect(object).to receive(:presigned_url).with(:get, { expires_in: 1800, virtual_host: true }) @dummy.avatar.expiring_url(1800, :thumb) end it "generates a url for the default style" do object = double allow(@dummy.avatar).to receive(:s3_object).with(:original).and_return(object) - expect(object).to receive(:presigned_url).with(:get, { expires_in: 1800 }) + expect(object).to receive(:presigned_url).with(:get, { expires_in: 1800, virtual_host: true }) @dummy.avatar.expiring_url(1800) end end From 0708634b4e31bae75b0a65ef045e35592ac0fadf Mon Sep 17 00:00:00 2001 From: "Mohammed O. Tillawy" Date: Thu, 14 Sep 2023 11:19:39 +0300 Subject: [PATCH 3/3] support virtual_host in presigned_url when using s3_host_alias when using options: { url: "s3_alias_url" & "s3_host_alias": "bucket_alias.domain.com" }, make the presigned_url : https://bucket_alias.domain.com/file?X-Amz-Credential= instead of: https://s3.amazonaws.com/bucket_alias.domain.com/file?X-Amz-Credential= --- lib/paperclip/storage/s3.rb | 5 ++++- spec/paperclip/storage/s3_spec.rb | 8 +++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/paperclip/storage/s3.rb b/lib/paperclip/storage/s3.rb index 70cf5fc70..70273e27e 100644 --- a/lib/paperclip/storage/s3.rb +++ b/lib/paperclip/storage/s3.rb @@ -187,7 +187,10 @@ def self.extended(base) def expiring_url(time = 3600, style_name = default_style) if path(style_name) - base_options = { expires_in: time } + base_options = { + expires_in: time, + virtual_host: @options[:url] == ":s3_alias_url" && @options[:s3_host_alias] != nil + } s3_object(style_name).presigned_url( :get, base_options.merge(s3_url_options) diff --git a/spec/paperclip/storage/s3_spec.rb b/spec/paperclip/storage/s3_spec.rb index f132d2121..2da845175 100644 --- a/spec/paperclip/storage/s3_spec.rb +++ b/spec/paperclip/storage/s3_spec.rb @@ -741,7 +741,7 @@ def counter object = double allow(@dummy.avatar).to receive(:s3_object).and_return(object) - expect(object).to receive(:presigned_url).with(:get, { expires_in: 3600 }) + expect(object).to receive(:presigned_url).with(:get, { expires_in: 3600, virtual_host: true }) @dummy.avatar.expiring_url end end @@ -761,6 +761,7 @@ def counter { expires_in: 3600, response_content_disposition: "inline", + virtual_host: true }, ) @dummy.avatar.expiring_url @@ -789,6 +790,7 @@ def counter { expires_in: 3600, response_content_type: "image/png", + virtual_host: true }, ) @dummy.avatar.expiring_url @@ -837,14 +839,14 @@ def counter it "generates a url for the thumb" do object = double allow(@dummy.avatar).to receive(:s3_object).with(:thumb).and_return(object) - expect(object).to receive(:presigned_url).with(:get, { expires_in: 1800 }) + expect(object).to receive(:presigned_url).with(:get, { expires_in: 1800, virtual_host: true }) @dummy.avatar.expiring_url(1800, :thumb) end it "generates a url for the default style" do object = double allow(@dummy.avatar).to receive(:s3_object).with(:original).and_return(object) - expect(object).to receive(:presigned_url).with(:get, { expires_in: 1800 }) + expect(object).to receive(:presigned_url).with(:get, { expires_in: 1800, virtual_host: true }) @dummy.avatar.expiring_url(1800) end end