From 78bec204248a43260a791b45aa1446bf90d70137 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Wed, 17 Aug 2016 16:16:00 +0800 Subject: [PATCH] FIX: Check options and not just site settings. --- lib/s3_helper.rb | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/s3_helper.rb b/lib/s3_helper.rb index ff628fbf8a7..8997f35d419 100644 --- a/lib/s3_helper.rb +++ b/lib/s3_helper.rb @@ -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