【发布时间】:2017-08-27 20:18:08
【问题描述】:
我在我的 rails 应用程序中使用带有 AWS 的 Paperclip。我有一个保存图像的模型:
class PaperclipFile < ActiveRecord::Base
before_validation :parse_image
belongs_to :attachment
attr_accessor :base64_attachment
has_attached_file :image, preserve_files: false
validates_attachment :image, presence: true
do_not_validate_attachment_file_type :image
private
def parse_image
return unless base64_attachment
image = Paperclip.io_adapters.for(base64_attachment)
image.original_filename = "img_#{SecureRandom.urlsafe_base64}.jpg"
end
end
基本上我可以在 base64 中传递一个图像,它会在 S3 上正确处理和存储它。问题是当我做 .destroy
p = PaperclipFile.first
p.destroy
之后,记录从数据库中消失,但文件并未从存储桶中销毁。我没有收到任何错误,所以它看起来根本不像在尝试。
输出是:
2.4.0 :005 > PaperclipFile.first.destroy
PaperclipFile Load (0.8ms) SELECT "paperclip_files".* FROM "paperclip_files" ORDER BY "paperclip_files"."id" ASC LIMIT $1 [["LIMIT", 1]]
(0.2ms) BEGIN
SQL (0.7ms) DELETE FROM "paperclip_files" WHERE "paperclip_files"."id" = $1 [["id", 2]]
(4.1ms) COMMIT
=> #<PaperclipFile id: 2, attachment_id: 2, created_at: "2017-04-03 04:02:11", updated_at: "2017-04-03 04:02:11", image_file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil>
2.4.0 :006 >
为了记录,我使用 Ruby 2.4、Rails 5.0.2 以及它们看起来像这样的 gem:
# Attachments
gem 'paperclip'
gem 'aws-sdk', '~> 2.3'
谢谢!
【问题讨论】:
标签: ruby-on-rails ruby amazon-s3 paperclip ruby-on-rails-5