问题
Carrierwave deletes files after the destruction of the object has been completed:
after_commit :remove_avatar! :on => :destroy
https://github.com/carrierwaveuploader/carrierwave
I have a worker that deletes the files. If one of the workers timeout when deleting a file from S3 I lose track of the files on S3 and my bucket becomes a mess (since my object that had the mounted file is gone from my DB).
How should I handle this?
Directly call remove_avatar!
before my object.destroy
and then skip_callback?
Is it safe?
回答1:
class User < ActiveRecord::Base
mount_uploader :avatar, AvatarUploader
before_destroy :clean_s3
private
def clean_s3
avatar.remove!
avatar.thumb.remove! # if you have thumb version or any other version
rescue Excon::Errors::Error => error
puts "Something gone wrong"
false
end
end
来源:https://stackoverflow.com/questions/22391183/carrierwave-destroy-object-only-after-mounted-files-have-been-deleted-from-sto