Удаляем следы загрузки в Amazon S3

This commit is contained in:
Nastia Gorokhova-Alekseeva
2022-05-27 18:55:48 +03:00
parent 8b6ea7e48b
commit f1d55852b2
3 changed files with 3 additions and 42 deletions

View File

@@ -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'

View File

@@ -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"

View File

@@ -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