Carrierwave - destroy object only after mounted files have been deleted from storage

雨燕双飞 提交于 2019-12-13 04:29:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!