diff --git a/config/amazon_s3.example.yml b/config/amazon_s3.example.yml index 68bb0cc..952f161 100644 --- a/config/amazon_s3.example.yml +++ b/config/amazon_s3.example.yml @@ -19,6 +19,9 @@ production: # Folder where attachment thumbnails are stored # thumbnails_folder: thumbnails/ + # Whether objects are private, needing presigned URLs (default false) + # private: true + development: # Copy configuration from `production` if desired diff --git a/lib/amazon_s3/configuration.rb b/lib/amazon_s3/configuration.rb index 4006b34..b2dc3b2 100644 --- a/lib/amazon_s3/configuration.rb +++ b/lib/amazon_s3/configuration.rb @@ -23,6 +23,7 @@ def initialize :region => nil, :attachments_folder => nil, :thumbnails_folder => nil, + :private => false, } end @@ -66,6 +67,10 @@ def region @config[:region] end + def private? + @config[:private] + end + def attachments_folder str = @config[:attachments_folder] if str.present? diff --git a/lib/amazon_s3/connection.rb b/lib/amazon_s3/connection.rb index fd1e2cf..b205e0e 100644 --- a/lib/amazon_s3/connection.rb +++ b/lib/amazon_s3/connection.rb @@ -46,7 +46,11 @@ def delete(filename, target_folder = @@config.attachments_folder) def object_url(filename, target_folder = @@config.attachments_folder) object = self.object(filename, target_folder) - object.public_url + if @@config.private? + object.presigned_url(:get) + else + object.public_url + end end def get(filename, target_folder = @@config.attachments_folder)