2015-05-25 03:57:06 -04:00
|
|
|
require "aws-sdk"
|
2014-09-24 16:52:09 -04:00
|
|
|
|
|
|
|
class S3Helper
|
|
|
|
|
2015-05-25 03:57:06 -04:00
|
|
|
def initialize(s3_bucket, tombstone_prefix=nil)
|
2014-09-24 16:52:09 -04:00
|
|
|
raise Discourse::InvalidParameters.new("s3_bucket") if s3_bucket.blank?
|
|
|
|
|
|
|
|
@s3_bucket = s3_bucket
|
|
|
|
@tombstone_prefix = tombstone_prefix
|
|
|
|
|
|
|
|
check_missing_site_settings
|
|
|
|
end
|
|
|
|
|
2016-08-14 23:21:24 -04:00
|
|
|
def upload(file, path, options={})
|
|
|
|
obj = s3_bucket.object(path)
|
2015-05-25 03:57:06 -04:00
|
|
|
obj.upload_file(file, options)
|
2014-09-24 16:52:09 -04:00
|
|
|
end
|
|
|
|
|
2016-08-12 05:18:19 -04:00
|
|
|
def remove(s3_filename, tombstone_filename=false)
|
2015-05-25 03:57:06 -04:00
|
|
|
bucket = s3_bucket
|
2016-08-14 23:21:24 -04:00
|
|
|
|
2014-09-24 16:52:09 -04:00
|
|
|
# copy the file in tombstone
|
2016-08-12 05:18:19 -04:00
|
|
|
if tombstone_filename && @tombstone_prefix.present?
|
2016-08-14 23:21:24 -04:00
|
|
|
bucket
|
2016-08-12 05:18:19 -04:00
|
|
|
.object(File.join(@tombstone_prefix, tombstone_filename))
|
|
|
|
.copy_from(copy_source: File.join(@s3_bucket, s3_filename))
|
2014-09-24 16:52:09 -04:00
|
|
|
end
|
2016-08-14 23:21:24 -04:00
|
|
|
|
2014-09-24 16:52:09 -04:00
|
|
|
# delete the file
|
2016-08-12 05:18:19 -04:00
|
|
|
bucket.object(s3_filename).delete
|
2015-05-25 03:57:06 -04:00
|
|
|
rescue Aws::S3::Errors::NoSuchKey
|
2014-09-24 16:52:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_tombstone_lifecycle(grace_period)
|
|
|
|
return if @tombstone_prefix.blank?
|
2015-05-25 11:59:00 -04:00
|
|
|
|
2014-09-24 16:52:09 -04:00
|
|
|
# cf. http://docs.aws.amazon.com/AmazonS3/latest/dev/object-lifecycle-mgmt.html
|
2015-05-25 03:57:06 -04:00
|
|
|
s3_resource.client.put_bucket_lifecycle({
|
|
|
|
bucket: @s3_bucket,
|
|
|
|
lifecycle_configuration: {
|
|
|
|
rules: [
|
|
|
|
{
|
2015-05-25 11:59:00 -04:00
|
|
|
id: "purge-tombstone",
|
|
|
|
status: "Enabled",
|
|
|
|
expiration: { days: grace_period },
|
2015-05-25 03:57:06 -04:00
|
|
|
prefix: @tombstone_prefix
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
})
|
2014-09-24 16:52:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2015-05-25 03:57:06 -04:00
|
|
|
def s3_resource
|
2015-05-26 09:54:25 -04:00
|
|
|
opts = { region: SiteSetting.s3_region }
|
2014-09-24 16:52:09 -04:00
|
|
|
|
2015-05-26 09:54:25 -04:00
|
|
|
unless SiteSetting.s3_use_iam_profile
|
|
|
|
opts[:access_key_id] = SiteSetting.s3_access_key_id
|
|
|
|
opts[:secret_access_key] = SiteSetting.s3_secret_access_key
|
|
|
|
end
|
2014-09-24 16:52:09 -04:00
|
|
|
|
2015-05-25 03:57:06 -04:00
|
|
|
Aws::S3::Resource.new(opts)
|
2014-09-24 16:52:09 -04:00
|
|
|
end
|
|
|
|
|
2015-05-25 03:57:06 -04:00
|
|
|
def s3_bucket
|
|
|
|
bucket = s3_resource.bucket(@s3_bucket)
|
|
|
|
bucket.create unless bucket.exists?
|
|
|
|
bucket
|
2014-09-24 16:52:09 -04:00
|
|
|
end
|
|
|
|
|
2015-05-25 03:57:06 -04:00
|
|
|
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
|
2014-09-24 16:52:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|