FIX: s3 endpoint when using 'us-east-1' region

This commit is contained in:
Régis Hanol 2015-05-27 17:50:49 +02:00
parent 5de9554510
commit 83d2b59fc3
3 changed files with 22 additions and 14 deletions

View File

@ -39,7 +39,12 @@ module FileStore
end end
def absolute_base_url def absolute_base_url
@absolute_base_url ||= "//#{s3_bucket}.s3-#{s3_region}.amazonaws.com" # cf. http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region
@absolute_base_url ||= if SiteSetting.s3_region == "us-east-1"
"//#{s3_bucket}.s3.amazonaws.com"
else
"//#{s3_bucket}.s3-#{SiteSetting.s3_region}.amazonaws.com"
end
end end
def external? def external?
@ -156,11 +161,6 @@ module FileStore
raise Discourse::SiteSettingMissing.new("s3_upload_bucket") if SiteSetting.s3_upload_bucket.blank? raise Discourse::SiteSettingMissing.new("s3_upload_bucket") if SiteSetting.s3_upload_bucket.blank?
@s3_bucket = SiteSetting.s3_upload_bucket.downcase @s3_bucket = SiteSetting.s3_upload_bucket.downcase
end end
def s3_region
SiteSetting.s3_region
end
end end
end end

View File

@ -27,7 +27,7 @@ describe FileStore::S3Store do
upload.stubs(:id).returns(42) upload.stubs(:id).returns(42)
upload.stubs(:extension).returns(".png") upload.stubs(:extension).returns(".png")
s3_helper.expects(:upload) s3_helper.expects(:upload)
expect(store.store_upload(uploaded_file, upload)).to eq("//s3_upload_bucket.s3-us-east-1.amazonaws.com/original/e/9/e9d71f5ee7c92d6dc9e92ffdad17b8bd49418f98.png") expect(store.store_upload(uploaded_file, upload)).to eq("//s3_upload_bucket.s3.amazonaws.com/original/e/9/e9d71f5ee7c92d6dc9e92ffdad17b8bd49418f98.png")
end end
end end
@ -37,7 +37,7 @@ describe FileStore::S3Store do
it "returns an absolute schemaless url" do it "returns an absolute schemaless url" do
optimized_image.stubs(:id).returns(42) optimized_image.stubs(:id).returns(42)
s3_helper.expects(:upload) s3_helper.expects(:upload)
expect(store.store_optimized_image(optimized_image_file, optimized_image)).to eq("//s3_upload_bucket.s3-us-east-1.amazonaws.com/optimized/8/6/86f7e437faa5a7fce15d1ddcb9eaeaea377667b8_100x200.png") expect(store.store_optimized_image(optimized_image_file, optimized_image)).to eq("//s3_upload_bucket.s3.amazonaws.com/optimized/8/6/86f7e437faa5a7fce15d1ddcb9eaeaea377667b8_100x200.png")
end end
end end
@ -63,11 +63,11 @@ describe FileStore::S3Store do
describe ".has_been_uploaded?" do describe ".has_been_uploaded?" do
it "identifies S3 uploads" do it "identifies S3 uploads" do
expect(store.has_been_uploaded?("//s3_upload_bucket.s3-us-east-1.amazonaws.com/1337.png")).to eq(true) expect(store.has_been_uploaded?("//s3_upload_bucket.s3.amazonaws.com/1337.png")).to eq(true)
end end
it "does not match other s3 urls" do it "does not match other s3 urls" do
expect(store.has_been_uploaded?("//s3_upload_bucket.s3.amazonaws.com/1337.png")).to eq(false) expect(store.has_been_uploaded?("//s3_upload_bucket.s3-us-east-1.amazonaws.com/1337.png")).to eq(false)
expect(store.has_been_uploaded?("//s3.amazonaws.com/s3_upload_bucket/1337.png")).to eq(false) expect(store.has_been_uploaded?("//s3.amazonaws.com/s3_upload_bucket/1337.png")).to eq(false)
expect(store.has_been_uploaded?("//s4_upload_bucket.s3.amazonaws.com/1337.png")).to eq(false) expect(store.has_been_uploaded?("//s4_upload_bucket.s3.amazonaws.com/1337.png")).to eq(false)
end end
@ -77,7 +77,15 @@ describe FileStore::S3Store do
describe ".absolute_base_url" do describe ".absolute_base_url" do
it "returns a lowercase schemaless absolute url" do it "returns a lowercase schemaless absolute url" do
expect(store.absolute_base_url).to eq("//s3_upload_bucket.s3-us-east-1.amazonaws.com") expect(store.absolute_base_url).to eq("//s3_upload_bucket.s3.amazonaws.com")
end
it "uses the proper endpoint" do
SiteSetting.stubs(:s3_region).returns("us-east-1")
expect(FileStore::S3Store.new(s3_helper).absolute_base_url).to eq("//s3_upload_bucket.s3.amazonaws.com")
SiteSetting.stubs(:s3_region).returns("us-east-2")
expect(FileStore::S3Store.new(s3_helper).absolute_base_url).to eq("//s3_upload_bucket.s3-us-east-2.amazonaws.com")
end end
end end

View File

@ -14,19 +14,19 @@ describe UserAvatarsController do
GlobalSetting.expects(:cdn_url).returns("http://awesome.com/boom") GlobalSetting.expects(:cdn_url).returns("http://awesome.com/boom")
upload = Fabricate(:upload, url: "//test.s3-#{SiteSetting.s3_region}.amazonaws.com/something") upload = Fabricate(:upload, url: "//test.s3.amazonaws.com/something")
Fabricate(:optimized_image, Fabricate(:optimized_image,
sha1: SecureRandom.hex << "A"*8, sha1: SecureRandom.hex << "A"*8,
upload: upload, upload: upload,
width: 98, width: 98,
height: 98, height: 98,
url: "//test.s3-#{SiteSetting.s3_region}.amazonaws.com/something/else") url: "//test.s3.amazonaws.com/something/else")
user = Fabricate(:user, uploaded_avatar_id: upload.id) user = Fabricate(:user, uploaded_avatar_id: upload.id)
get :show, size: 97, username: user.username, version: upload.id, hostname: 'default' get :show, size: 97, username: user.username, version: upload.id, hostname: 'default'
# 98 is closese which is 49 * 2 for retina # 98 is closest which is 49 * 2 for retina
expect(response).to redirect_to("http://awesome.com/boom/user_avatar/default/#{user.username_lower}/98/#{upload.id}.png") expect(response).to redirect_to("http://awesome.com/boom/user_avatar/default/#{user.username_lower}/98/#{upload.id}.png")
get :show, size: 98, username: user.username, version: upload.id, hostname: 'default' get :show, size: 98, username: user.username, version: upload.id, hostname: 'default'