diff --git a/config/initializers/paperclip.rb b/config/initializers/paperclip.rb index 392784c53..c02f3afec 100644 --- a/config/initializers/paperclip.rb +++ b/config/initializers/paperclip.rb @@ -13,26 +13,32 @@ Paperclip::Attachment.default_options.merge!( Paperclip::UriAdapter.register -if ENV['PAPERCLIP_STORAGE'] == "s3" - - if ENV['S3_BUCKET'].nil? or ENV['AWS_REGION'].nil? or - ENV['AWS_ACCESS_KEY_ID'].nil? or ENV['AWS_SECRET_ACCESS_KEY'].nil? - puts "WARNING! Environment variables S3_BUCKET, AWS_REGION, " + - "AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY must be set." +if ENV['PAPERCLIP_STORAGE'] == 's3' + if ENV['S3_BUCKET'].nil? || ENV['AWS_REGION'].nil? || + ENV['AWS_ACCESS_KEY_ID'].nil? || ENV['AWS_SECRET_ACCESS_KEY'].nil? + puts 'WARNING! Environment variables S3_BUCKET, AWS_REGION, '\ + 'AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY must be set.' exit 1 end - Paperclip::Attachment.default_options.merge!({ + + s3_credentials = { bucket: ENV['S3_BUCKET'] } + + if ENV['AWS_ACCESS_KEY_ID'] && ENV['AWS_SECRET_ACCESS_KEY'] + s3_credentials[:access_key_id] = ENV['AWS_ACCESS_KEY_ID'] + s3_credentials[:secret_access_key] = ENV['AWS_SECRET_ACCESS_KEY'] + end + + s3_path = '/:class/:attachment/:id_partition/:hash/:style/:filename' + s3_path.prepend("/#{ENV['S3_SUBFOLDER']}") if ENV['S3_SUBFOLDER'] + + Paperclip::Attachment.default_options.merge!( url: ':s3_domain_url', - path: '/:class/:attachment/:id_partition/:hash/:style/:filename', + path: s3_path, storage: :s3, s3_region: ENV['AWS_REGION'], s3_host_name: "s3.#{ENV['AWS_REGION']}.amazonaws.com", s3_protocol: 'https', - s3_credentials: { - bucket: ENV['S3_BUCKET'], - access_key_id: ENV['AWS_ACCESS_KEY_ID'], - secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'] - }, + s3_credentials: s3_credentials, s3_permissions: { original: :private, medium: :private @@ -43,12 +49,9 @@ if ENV['PAPERCLIP_STORAGE'] == "s3" icon: :REDUCED_REDUNDANCY, icon_small: :REDUCED_REDUNDANCY } - }) -elsif ENV['PAPERCLIP_STORAGE'] == "filesystem" - Paperclip::Attachment.default_options.merge!({ - storage: :filesystem - }) - + ) +elsif ENV['PAPERCLIP_STORAGE'] == 'filesystem' + Paperclip::Attachment.default_options[:storage] = :filesystem end Paperclip::Attachment.class_eval do