FIX: Check options and not just site settings.

This commit is contained in:
Guo Xiang Tan 2016-08-17 16:16:00 +08:00
parent 79c1d3459b
commit 78bec20424
1 changed files with 13 additions and 5 deletions

View File

@ -2,8 +2,10 @@ require "aws-sdk"
class S3Helper
def initialize(s3_upload_bucket, tombstone_prefix='', s3_options=default_s3_options)
@s3_options = s3_options
class SettingMissing < StandardError; end
def initialize(s3_upload_bucket, tombstone_prefix='', options={})
@s3_options = default_s3_options.merge(options)
@s3_bucket, @s3_bucket_folder_path = begin
raise Discourse::InvalidParameters.new("s3_bucket") if s3_upload_bucket.blank?
@ -16,6 +18,8 @@ class S3Helper
else
tombstone_prefix
end
check_missing_options
end
def upload(file, path, options={})
@ -70,9 +74,6 @@ class S3Helper
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
@ -89,4 +90,11 @@ class S3Helper
bucket.create unless bucket.exists?
bucket
end
def check_missing_options
unless SiteSetting.s3_use_iam_profile
raise SettingMissing.new("access_key_id") if @s3_options[:access_key_id].blank?
raise SettingMissing.new("secret_access_key") if @s3_options[:secret_access_key].blank?
end
end
end