From 7ff1f6cb9deffddfeed4162644540864f2bc50a1 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Tue, 16 Aug 2016 15:23:48 +0800 Subject: [PATCH] Allow custom bucket name for `FileStore::S3Store`. --- lib/file_store/s3_store.rb | 19 +++++++------------ spec/components/file_store/s3_store_spec.rb | 6 ------ 2 files changed, 7 insertions(+), 18 deletions(-) diff --git a/lib/file_store/s3_store.rb b/lib/file_store/s3_store.rb index cbfee317f67..0615dbc52d3 100644 --- a/lib/file_store/s3_store.rb +++ b/lib/file_store/s3_store.rb @@ -6,12 +6,11 @@ require_dependency "file_helper" module FileStore class S3Store < BaseStore - attr_reader :s3_bucket - TOMBSTONE_PREFIX ||= "tombstone/" - def initialize(s3_helper=nil) - @s3_helper = s3_helper || S3Helper.new(s3_bucket, TOMBSTONE_PREFIX) + def initialize(s3_helper=nil, s3_bucket=get_s3_bucket) + @s3_bucket = s3_bucket + @s3_helper = s3_helper || S3Helper.new(@s3_bucket, TOMBSTONE_PREFIX) end def store_upload(file, upload, content_type = nil) @@ -63,7 +62,7 @@ module FileStore end def absolute_base_url - bucket = s3_bucket.split("/".freeze, 2).first + bucket = @s3_bucket.split("/".freeze, 2).first # cf. http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region @absolute_base_url ||= if SiteSetting.s3_region == "us-east-1" @@ -104,13 +103,9 @@ module FileStore UserAvatar.external_avatar_url(user_id, avatar.upload_id, avatar.width) end - def s3_bucket - @s3_bucket ||= begin - raise Discourse::SiteSettingMissing.new("s3_upload_bucket") if SiteSetting.s3_upload_bucket.blank? - SiteSetting.s3_upload_bucket.downcase - end + def get_s3_bucket + raise Discourse::SiteSettingMissing.new("s3_upload_bucket") if SiteSetting.s3_upload_bucket.blank? + SiteSetting.s3_upload_bucket.downcase end - end - end diff --git a/spec/components/file_store/s3_store_spec.rb b/spec/components/file_store/s3_store_spec.rb index 49ad6af2c53..97bad8fafea 100644 --- a/spec/components/file_store/s3_store_spec.rb +++ b/spec/components/file_store/s3_store_spec.rb @@ -256,10 +256,4 @@ describe FileStore::S3Store do assert_path("https://hello", nil) end end - - describe "#s3_bucket" do - it "should return the right bucket name" do - expect(store.s3_bucket).to eq('s3-upload-bucket') - end - end end