Allow custom bucket name for FileStore::S3Store.

This commit is contained in:
Guo Xiang Tan 2016-08-16 15:23:48 +08:00
parent 416e7e0d1e
commit 7ff1f6cb9d
2 changed files with 7 additions and 18 deletions

View file

@ -6,12 +6,11 @@ require_dependency "file_helper"
module FileStore module FileStore
class S3Store < BaseStore class S3Store < BaseStore
attr_reader :s3_bucket
TOMBSTONE_PREFIX ||= "tombstone/" TOMBSTONE_PREFIX ||= "tombstone/"
def initialize(s3_helper=nil) def initialize(s3_helper=nil, s3_bucket=get_s3_bucket)
@s3_helper = s3_helper || S3Helper.new(s3_bucket, TOMBSTONE_PREFIX) @s3_bucket = s3_bucket
@s3_helper = s3_helper || S3Helper.new(@s3_bucket, TOMBSTONE_PREFIX)
end end
def store_upload(file, upload, content_type = nil) def store_upload(file, upload, content_type = nil)
@ -63,7 +62,7 @@ module FileStore
end end
def absolute_base_url 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 # cf. http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region
@absolute_base_url ||= if SiteSetting.s3_region == "us-east-1" @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) UserAvatar.external_avatar_url(user_id, avatar.upload_id, avatar.width)
end end
def s3_bucket def get_s3_bucket
@s3_bucket ||= begin 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? SiteSetting.s3_upload_bucket.downcase
SiteSetting.s3_upload_bucket.downcase
end
end end
end end
end end

View file

@ -256,10 +256,4 @@ describe FileStore::S3Store do
assert_path("https://hello", nil) assert_path("https://hello", nil)
end end
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 end