Carrierwave - 销毁对象只有安装后的文件已被删除的存储 [英] Carrierwave - destroy object only after mounted files have been deleted from storage

查看:209
本文介绍了Carrierwave - 销毁对象只有安装后的文件已被删除的存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Carrierwave删除的文件的对象的破坏已经完成之后:

  after_commit:remove_avatar! :上=> :破坏
 

https://github.com/carrierwaveuploader/carrierwave

我有一个工人,删除的文件。如果一个工人删除从S3文件时超时,我失去了赛道上S3的文件和我斗变得一团糟(因为我的对象,有安装的文件是从我的数据库了)。

我应该如何处理呢? 直接调用 remove_avatar!在我的 object.destroy 然后 skip_callback?是否安全?

解决方案

 类用户的LT;的ActiveRecord :: Base的
  mount_uploader:头像,AvatarUploader

  before_destroy:clean_s3

私人
  高清clean_s3
    avatar.remove!
    avatar.thumb.remove! #如果你有拇指版或任何其他版本
  抢救EXCON ::错误::错误=>错误
    把东西出了错
    假
  结束
结束
 

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?

解决方案

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

这篇关于Carrierwave - 销毁对象只有安装后的文件已被删除的存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆