From 96c4a6c8afd8531023b07cb209c3ebef5ef947d9 Mon Sep 17 00:00:00 2001 From: Nataliya Terskaya Date: Mon, 23 May 2022 16:03:14 +0200 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=D0=B0=20=D0=BF=D1=80=D0=B8=D0=BC=D0=B5=D1=80=20=D0=BA=D0=BE?= =?UTF-8?q?=D0=BD=D1=84=D0=B8=D0=B3=D0=B0=20=D0=B8=20=D0=B8=D1=81=D0=BF?= =?UTF-8?q?=D1=80=D0=B0=D0=B2=D0=B8=D0=BB=D0=B0=20=D1=82=D0=B5=D0=BA=D1=81?= =?UTF-8?q?=D1=82=20=D0=BA=D0=BE=D0=BC=D0=BC=D0=B5=D0=BD=D1=82=D0=B0=D1=80?= =?UTF-8?q?=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../no_cached_s3/config_example.rb.example | 61 +++++++++++++++++++ lib/paperclip/storage/no_cache_s3.rb | 2 +- 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 config_examples/no_cached_s3/config_example.rb.example diff --git a/config_examples/no_cached_s3/config_example.rb.example b/config_examples/no_cached_s3/config_example.rb.example new file mode 100644 index 0000000..85947f5 --- /dev/null +++ b/config_examples/no_cached_s3/config_example.rb.example @@ -0,0 +1,61 @@ +# frozen_string_literal: true + +STATIC_CDN = Rails.env.development? ? 'http://localhost:3000' : 'https://static-ru.insales.ru' + +# Fog::Storage требует разные префиксы для разных S3 стораджев. +# Этот хэлпер использует поле с префиксом, если есть, или добавляет его. +prefix_credentials = lambda do |prefix, input| + result = input.symbolize_keys + %i[access_key_id secret_access_key].each do |field| + key = :"#{prefix}_#{field}" + result[key] ||= result.delete(field) if result[field] + end + result +end + +fog_directory = lambda do |bucket:, **options| + Fog::Storage.new(options).directories.new(key: bucket, public: true) +end + +PAPERCLIP_NO_CACHE_S3_CONFIG_BASE = + case Rails.env + when 'production', 'test' + { + storage: :no_cache_s3, + # Не скачиваем файлы по http в тестах, чтобы не делать стабы vcr для всего подряд. + # В продакшене файлы скачиваются по url, чтобы качать через cdn и не тратить облачный трафик. + to_file_using_fog: Rails.env.test?, + url: "#{STATIC_CDN}/:key", # Умный cdn сам решает куда идти за файлом. + stores: { + yandex: fog_directory[ + provider: 'AWS', + region: 'ru-central1', + endpoint: 'https://storage.yandexcloud.net', + **prefix_credentials[:aws, Rails.application.secrets.yandex_cloud], + bucket: 'digital-static' + ], + sbercloud: fog_directory[ + provider: 'AWS', + region: 'ru-moscow', + endpoint: 'https://obs.ru-moscow-1.hc.sbercloud.ru', + **prefix_credentials[:aws, Rails.application.secrets.sbercloud], + bucket: 'digital-static' + ] + } + }.freeze + else + { + storage: :no_cache_s3, + url: '/:key', + stores: { + s3: fog_directory[ + provider: 'Local', + local_root: Rails.root.join('public').to_s, + bucket: '' + ] + } + }.freeze + end + +# Добавляет общий префикс ко всем ключам. +PAPERCLIP_NO_CACHE_S3_CONFIG = ->(key) { PAPERCLIP_NO_CACHE_S3_CONFIG_BASE.merge(key: "digital/#{key}") } diff --git a/lib/paperclip/storage/no_cache_s3.rb b/lib/paperclip/storage/no_cache_s3.rb index 63436dc..8cd4ffb 100644 --- a/lib/paperclip/storage/no_cache_s3.rb +++ b/lib/paperclip/storage/no_cache_s3.rb @@ -74,7 +74,7 @@ module Paperclip end # If store_id is given, it forces download from specific store using fog interface. - # Otherway it tries to download from cache store and finally uses url to download file + # Otherway uses url to download file # via HTTP. This is the most compatible way to delayeds3. def to_file(style = default_style, store_id = nil) style_key = key(style)