From 47b0c6185356d4a2533b3ef96e9775a3a82da079 Mon Sep 17 00:00:00 2001 From: Yamagishi Kazutoshi Date: Tue, 7 Nov 2017 22:30:31 +0900 Subject: [PATCH] Unify file upload to using fog (#5604) --- Gemfile | 6 ++- Gemfile.lock | 23 ++++---- config/initializers/paperclip.rb | 92 +++++++++++++++++++------------- 3 files changed, 71 insertions(+), 50 deletions(-) diff --git a/Gemfile b/Gemfile index e535372f9..d0b7aaef1 100644 --- a/Gemfile +++ b/Gemfile @@ -14,8 +14,10 @@ gem 'pg', '~> 0.20' gem 'pghero', '~> 1.7' gem 'dotenv-rails', '~> 2.2' -gem 'aws-sdk', '~> 2.9' -gem 'fog-openstack', '~> 0.1' +gem 'fog-aws', '~> 1.4', require: false +gem 'fog-core', '~> 1.45' +gem 'fog-local', '~> 0.4', require: false +gem 'fog-openstack', '~> 0.1', require: false gem 'paperclip', '~> 5.1' gem 'paperclip-av-transcoder', '~> 0.6' diff --git a/Gemfile.lock b/Gemfile.lock index 13ca840bb..f9c69d538 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -57,14 +57,6 @@ GEM encryptor (~> 3.0.0) av (0.9.0) cocaine (~> 0.5.3) - aws-sdk (2.10.73) - aws-sdk-resources (= 2.10.73) - aws-sdk-core (2.10.73) - aws-sigv4 (~> 1.0) - jmespath (~> 1.0) - aws-sdk-resources (2.10.73) - aws-sdk-core (= 2.10.73) - aws-sigv4 (1.0.2) bcrypt (3.1.11) better_errors (2.4.0) coderay (>= 1.0.0) @@ -160,6 +152,11 @@ GEM i18n (~> 0.5) fast_blank (1.0.0) ffi (1.9.18) + fog-aws (1.4.1) + fog-core (~> 1.38) + fog-json (~> 1.0) + fog-xml (~> 0.1) + ipaddress (~> 0.8) fog-core (1.45.0) builder excon (~> 0.58) @@ -167,10 +164,15 @@ GEM fog-json (1.0.2) fog-core (~> 1.0) multi_json (~> 1.10) + fog-local (0.4.0) + fog-core (~> 1.27) fog-openstack (0.1.22) fog-core (>= 1.40) fog-json (>= 1.0) ipaddress (>= 0.8) + fog-xml (0.1.3) + fog-core + nokogiri (>= 1.5.11, < 2.0.0) formatador (0.2.5) fuubar (2.2.0) rspec-core (~> 3.0) @@ -226,7 +228,6 @@ GEM idn-ruby (0.1.0) ipaddress (0.8.3) iso-639 (0.2.8) - jmespath (1.3.1) json (2.1.0) json-ld (2.1.7) multi_json (~> 1.12) @@ -538,7 +539,6 @@ DEPENDENCIES active_record_query_trace (~> 1.5) addressable (~> 2.5) annotate (~> 2.7) - aws-sdk (~> 2.9) better_errors (~> 2.4) binding_of_caller (~> 0.7) bootsnap @@ -561,6 +561,9 @@ DEPENDENCIES fabrication (~> 2.18) faker (~> 1.7) fast_blank (~> 1.0) + fog-aws (~> 1.4) + fog-core (~> 1.45) + fog-local (~> 0.4) fog-openstack (~> 0.1) fuubar (~> 2.2) goldfinger (~> 2.0) diff --git a/config/initializers/paperclip.rb b/config/initializers/paperclip.rb index 2c82a91db..9e846bf6b 100644 --- a/config/initializers/paperclip.rb +++ b/config/initializers/paperclip.rb @@ -7,60 +7,76 @@ Paperclip.interpolates :filename do |attachment, style| [basename(attachment, style), extension(attachment, style)].delete_if(&:blank?).join('.') end -Paperclip::Attachment.default_options[:use_timestamp] = false +Paperclip::Attachment.default_options.merge!( + use_timestamp: false, + path: ':class/:attachment/:id_partition/:style/:filename', + storage: :fog +) if ENV['S3_ENABLED'] == 'true' - Aws.eager_autoload!(services: %w(S3)) + require 'fog/aws' - Paperclip::Attachment.default_options[:storage] = :s3 - Paperclip::Attachment.default_options[:s3_protocol] = ENV.fetch('S3_PROTOCOL') { 'https' } - Paperclip::Attachment.default_options[:url] = ':s3_domain_url' - Paperclip::Attachment.default_options[:s3_host_name] = ENV.fetch('S3_HOSTNAME') { "s3-#{ENV.fetch('S3_REGION')}.amazonaws.com" } - Paperclip::Attachment.default_options[:path] = '/:class/:attachment/:id_partition/:style/:filename' - Paperclip::Attachment.default_options[:s3_headers] = { 'Cache-Control' => 'max-age=315576000' } - Paperclip::Attachment.default_options[:s3_permissions] = ENV.fetch('S3_PERMISSION') { 'public-read' } - Paperclip::Attachment.default_options[:s3_region] = ENV.fetch('S3_REGION') { 'us-east-1' } + s3_protocol = ENV.fetch('S3_PROTOCOL') { 'https' } + s3_hostname = ENV.fetch('S3_HOSTNAME') { "s3-#{ENV['S3_REGION']}.amazonaws.com" } + aws_signature_version = ENV['S3_SIGNATURE_VERSION'] == 's3' ? 2 : ENV['S3_SIGNATURE_VERSION'].to_i + aws_signature_version = 4 if aws_signature_version.zero? - Paperclip::Attachment.default_options[:s3_credentials] = { - bucket: ENV.fetch('S3_BUCKET'), - access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'), - secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'), - } - - unless ENV['S3_ENDPOINT'].blank? - Paperclip::Attachment.default_options[:s3_options] = { - endpoint: ENV['S3_ENDPOINT'], - signature_version: ENV['S3_SIGNATURE_VERSION'] || 'v4', - force_path_style: true, + Paperclip::Attachment.default_options.merge!( + fog_credentials: { + provider: 'AWS', + aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'], + aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'], + aws_signature_version: aws_signature_version, + region: ENV.fetch('S3_REGION') { 'us-east-1' }, + scheme: s3_protocol, + host: s3_hostname + }, + fog_directory: ENV['S3_BUCKET'], + fog_options: { + acl: ENV.fetch('S3_PERMISSION') { 'public-read' }, + cache_control: 'max-age=315576000', } + ) - Paperclip::Attachment.default_options[:url] = ':s3_path_url' + if ENV.has_key?('S3_ENDPOINT') + Paperclip::Attachment.default_options[:fog_credentials].merge!( + endpoint: ENV['S3_ENDPOINT'], + path_style: true + ) + Paperclip::Attachment.default_options[:fog_host] = "#{s3_protocol}://#{s3_hostname}/#{ENV['S3_BUCKET']}" end - unless ENV['S3_CLOUDFRONT_HOST'].blank? - Paperclip::Attachment.default_options[:url] = ':s3_alias_url' - Paperclip::Attachment.default_options[:s3_host_alias] = ENV['S3_CLOUDFRONT_HOST'] + if ENV.has_key?('S3_CLOUDFRONT_HOST') + Paperclip::Attachment.default_options[:fog_host] = "#{s3_protocol}://#{ENV['S3_CLOUDFRONT_HOST']}" end elsif ENV['SWIFT_ENABLED'] == 'true' + require 'fog/openstack' + Paperclip::Attachment.default_options.merge!( - path: ':class/:attachment/:id_partition/:style/:filename', - storage: :fog, fog_credentials: { provider: 'OpenStack', - openstack_username: ENV.fetch('SWIFT_USERNAME'), - openstack_project_name: ENV.fetch('SWIFT_TENANT'), - openstack_tenant: ENV.fetch('SWIFT_TENANT'), # Some OpenStack-v2 ignores project_name but needs tenant - openstack_api_key: ENV.fetch('SWIFT_PASSWORD'), - openstack_auth_url: ENV.fetch('SWIFT_AUTH_URL'), - openstack_domain_name: ENV['SWIFT_DOMAIN_NAME'] || 'default', + openstack_username: ENV['SWIFT_USERNAME'], + openstack_project_name: ENV['SWIFT_TENANT'], + openstack_tenant: ENV['SWIFT_TENANT'], # Some OpenStack-v2 ignores project_name but needs tenant + openstack_api_key: ENV['SWIFT_PASSWORD'], + openstack_auth_url: ENV['SWIFT_AUTH_URL'], + openstack_domain_name: ENV.fetch('SWIFT_DOMAIN_NAME') { 'default' }, openstack_region: ENV['SWIFT_REGION'], - openstack_cache_ttl: ENV['SWIFT_CACHE_TTL'] || 60, + openstack_cache_ttl: ENV.fetch('SWIFT_CACHE_TTL') { 60 }, }, - fog_directory: ENV.fetch('SWIFT_CONTAINER'), - fog_host: ENV['SWIFT_OBJECT_URL'], + fog_directory: ENV['SWIFT_CONTAINER'], + fog_host: ENV['SWIIFT_OBJECT_URL'], fog_public: true ) else - Paperclip::Attachment.default_options[:path] = (ENV['PAPERCLIP_ROOT_PATH'] || ':rails_root/public/system') + '/:class/:attachment/:id_partition/:style/:filename' - Paperclip::Attachment.default_options[:url] = (ENV['PAPERCLIP_ROOT_URL'] || '/system') + '/:class/:attachment/:id_partition/:style/:filename' + require 'fog/local' + + Paperclip::Attachment.default_options.merge!( + fog_credentials: { + provider: 'Local', + local_root: ENV.fetch('PAPERCLIP_ROOT_PATH') { Rails.root.join('public', 'system') }, + }, + fog_directory: '', + fog_host: ENV.fetch('PAPERCLIP_ROOT_URL') { '/system' } + ) end