From f1d55852b2b014326f05a6fc7eede46959af007c Mon Sep 17 00:00:00 2001 From: Nastia Gorokhova-Alekseeva Date: Fri, 27 May 2022 18:55:48 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A3=D0=B4=D0=B0=D0=BB=D1=8F=D0=B5=D0=BC=20?= =?UTF-8?q?=D1=81=D0=BB=D0=B5=D0=B4=D1=8B=20=D0=B7=D0=B0=D0=B3=D1=80=D1=83?= =?UTF-8?q?=D0=B7=D0=BA=D0=B8=20=D0=B2=20Amazon=20S3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/paperclip/storage/delayeds3.rb | 40 +----------------------------- test/integration_test.rb | 4 +-- test/storage_test.rb | 1 - 3 files changed, 3 insertions(+), 42 deletions(-) diff --git a/lib/paperclip/storage/delayeds3.rb b/lib/paperclip/storage/delayeds3.rb index d5dd304..f24e7af 100644 --- a/lib/paperclip/storage/delayeds3.rb +++ b/lib/paperclip/storage/delayeds3.rb @@ -36,7 +36,6 @@ module Paperclip module ClassMethods attr_reader :s3_url_template, :s3_path_template, :filesystem_url_template, :filesystem_path_template, - :s3_credentials, :s3_bucket, :synced_to_s3_field, :synced_to_yandex_field, :yandex_bucket_name, :yandex_credentials, :synced_to_sbercloud_field, @@ -51,29 +50,16 @@ module Paperclip @filesystem_url_template = options[:filesystem_url] @filesystem_path_template = options[:filesystem_path] - @s3_credentials = Delayeds3.parse_credentials(options[:s3_credentials]) @yandex_credentials = Delayeds3.parse_credentials(options[:yandex_credentials]) @sbercloud_credentials = Delayeds3.parse_credentials(options[:sbercloud_credentials]) - @s3_bucket = options[:bucket] || @s3_credentials[:bucket] @yandex_bucket_name = options[:yandex_bucket] @sbercloud_bucket_name = options[:sbercloud_bucket] - @synced_to_s3_field ||= :"#{attachment_name}_synced_to_s3" @synced_to_yandex_field ||= :"#{attachment_name}_synced_to_yandex" @synced_to_sbercloud_field ||= :"#{attachment_name}_synced_to_sbercloud" end - def aws_bucket - @aws_bucket ||= begin - params = s3_credentials.reject { |_k, v| v.blank? } - params[:region] ||= 'us-east-1' - s3_client = Aws::S3::Client.new(params) - s3_resource = Aws::S3::Resource.new(client: s3_client) - s3_resource.bucket(s3_bucket) - end - end - def yandex_bucket @yandex_bucket ||= begin params = yandex_credentials.reject { |_k, v| v.blank? } @@ -95,7 +81,7 @@ module Paperclip end end - delegate :synced_to_s3_field, :synced_to_yandex_field, :synced_to_sbercloud_field, to: :class + delegate :synced_to_yandex_field, :synced_to_sbercloud_field, to: :class def initialize(*) super @@ -141,8 +127,6 @@ module Paperclip case store_id when :cache File.exist?(filesystem_path(style)) - when :s3 - self.class.aws_bucket.object(s3_path(style)).exists? when :yandex self.class.yandex_bucket.object(s3_path(style)).exists? when :sbercloud @@ -170,27 +154,6 @@ module Paperclip Paperclip::Upfile.content_type_from_file path end - def write_to_s3 - return true if instance_read(:synced_to_s3) - paths = filesystem_paths - if paths.length < styles.length || paths.empty? # To make monitoring easier - raise "Local files not found for #{instance.class.name}:#{instance.id}" - end - paths.each do |style, file| - log("saving to s3 #{file}") - content_type = style == :original ? instance_read(:content_type) : file_content_type(file) - s3_object = self.class.aws_bucket.object(s3_path(style)) - s3_object.upload_file(file, - cache_control: "max-age=#{10.year.to_i}", - content_type: content_type, - expires: 10.year.from_now.httpdate, - acl: 'public-read') - end - if instance.class.unscoped.where(id: instance.id).update_all(synced_to_s3_field => true) == 1 - instance.touch - end - end - def write_to_yandex return true if instance_read(:synced_to_yandex) paths = filesystem_paths @@ -301,7 +264,6 @@ module Paperclip def upload_to(store_id) case store_id.to_s - when 's3' then write_to_s3 when 'yandex' then write_to_yandex when 'sbercloud' then write_to_sbercloud else raise 'Unknown store id' diff --git a/test/integration_test.rb b/test/integration_test.rb index 25f83cc..93ea548 100644 --- a/test/integration_test.rb +++ b/test/integration_test.rb @@ -324,10 +324,10 @@ class IntegrationTest < Test::Unit::TestCase rebuild_model :styles => { :large => "300x300>", :medium => "100x100", :thumb => ["32x32#", :gif] }, - :storage => :s3, + :storage => :yandex, :whiny_thumbnails => true, # :s3_options => {:logger => Logger.new(StringIO.new)}, - :s3_credentials => File.new(File.join(File.dirname(__FILE__), "s3.yml")), + :yandex_credentials => File.new(File.join(File.dirname(__FILE__), "s3.yml")), :default_style => :medium, :bucket => ENV['S3_TEST_BUCKET'], :path => ":class/:attachment/:id/:style/:basename.:extension" diff --git a/test/storage_test.rb b/test/storage_test.rb index c56ae32..1885737 100644 --- a/test/storage_test.rb +++ b/test/storage_test.rb @@ -8,7 +8,6 @@ class StorageTest < Test::Unit::TestCase rebuild_model storage: :delayeds3, bucket: 'testing', path: ':attachment/:style/:basename.:extension', - s3_credentials: {}, yandex_credentials: {}, sbercloud_credentials: {} end