Allow custom s3 options for `S3Helper`.

This commit is contained in:
Guo Xiang Tan 2016-08-16 11:13:59 +08:00
parent 37b68f10ae
commit 11356fb9f4
1 changed files with 8 additions and 12 deletions

View File

@ -2,7 +2,9 @@ require "aws-sdk"
class S3Helper
def initialize(s3_upload_bucket, tombstone_prefix='')
def initialize(s3_upload_bucket, tombstone_prefix='', s3_options=default_s3_options)
@s3_options = s3_options
@s3_bucket, @s3_bucket_folder_path = begin
raise Discourse::InvalidParameters.new("s3_bucket") if s3_upload_bucket.blank?
s3_upload_bucket.downcase.split("/".freeze, 2)
@ -14,8 +16,6 @@ class S3Helper
else
tombstone_prefix
end
check_missing_site_settings
end
def upload(file, path, options={})
@ -66,10 +66,13 @@ class S3Helper
path
end
def s3_options
def default_s3_options
opts = { region: SiteSetting.s3_region }
unless SiteSetting.s3_use_iam_profile
raise Discourse::SiteSettingMissing.new("s3_access_key_id") if SiteSetting.s3_access_key_id.blank?
raise Discourse::SiteSettingMissing.new("s3_secret_access_key") if SiteSetting.s3_secret_access_key.blank?
opts[:access_key_id] = SiteSetting.s3_access_key_id
opts[:secret_access_key] = SiteSetting.s3_secret_access_key
end
@ -78,7 +81,7 @@ class S3Helper
end
def s3_resource
Aws::S3::Resource.new(s3_options)
Aws::S3::Resource.new(@s3_options)
end
def s3_bucket
@ -86,11 +89,4 @@ class S3Helper
bucket.create unless bucket.exists?
bucket
end
def check_missing_site_settings
unless SiteSetting.s3_use_iam_profile
raise Discourse::SiteSettingMissing.new("s3_access_key_id") if SiteSetting.s3_access_key_id.blank?
raise Discourse::SiteSettingMissing.new("s3_secret_access_key") if SiteSetting.s3_secret_access_key.blank?
end
end
end