我在rails应用程序中使用Paperclip和AWS。我有一个模型来保存图像:
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和gems看起来像这样:
# Attachments
gem 'paperclip'
gem 'aws-sdk', '~> 2.3'
谢谢!
答案 0 :(得分:0)
您编写的代码没有任何问题。
如果您想从S3中删除图像,只需进行以下更改 -
p = PaperclipFile.first
p.destroy # Removes the attachment from DB
p.clear # Deletes the attachment from S3
p.save # To save the model.
答案 1 :(得分:0)
默认preserve_files设置为false。尝试删除选项,如下所示。
long audioPlayTime = 1000; //clip duration in miliseconds.
try {
Thread.sleep(audioPlayTime);
} catch (InterruptedException ex){
//TODO
}
答案 2 :(得分:0)
我对destroy没有任何问题,但是我使用了form = document.querySelector('form');
Rails.fire(form, 'submit');
,它不会触发回调,并且效果与您看到的相同。
另一个可能的原因可能是缺少删除文件的许可。 直接在ruby s3 sdk上检查删除。