需要删除丢失文件的CarrierWave附件

时间:2014-06-23 19:08:44

标签: carrierwave

我们正在使用较旧的Carrierwave安装,但有一个问题导致我们的许多虚拟形象从S3存储桶中删除,但它们仍然被Carrierwave引用。如何在不破坏其他用户头像的情况下安全地清除这些破坏的实例?

版本信息:

Ruby 2.1.0

carrierwave-0.5.8
excon-0.32.0
fog-0.7.2
railties-3.2.18

此时我无法进一步升级gem,因为还有其他依赖项会引入重大变化。

我试过了instance.remove_avatar!功能但失败并出现以下错误。

irb(main):041:0> t.remove_avatar!
Digest::Digest is deprecated; use Digest
[excon][WARNING] Invalid Excon request keys: :host
/app/vendor/bundle/ruby/2.1.0/gems/excon-0.32.0/lib/excon/connection.rb:389:in `validate_params'
/app/vendor/bundle/ruby/2.1.0/gems/excon-0.32.0/lib/excon/connection.rb:225:in `request'
/app/vendor/bundle/ruby/2.1.0/gems/fog-0.7.2/lib/fog/core/connection.rb:20:in `request'
/app/vendor/bundle/ruby/2.1.0/gems/fog-0.7.2/lib/fog/storage/aws.rb:323:in `request'
/app/vendor/bundle/ruby/2.1.0/gems/fog-0.7.2/lib/fog/storage/requests/aws/delete_object.rb:20:in `delete_object'
/app/vendor/bundle/ruby/2.1.0/gems/fog-0.7.2/lib/fog/storage/models/aws/file.rb:58:in `destroy'
/app/vendor/bundle/ruby/2.1.0/gems/carrierwave-0.5.8/lib/carrierwave/storage/fog.rb:178:in `delete'
/app/vendor/bundle/ruby/2.1.0/gems/carrierwave-0.5.8/lib/carrierwave/uploader/remove.rb:15:in `block in remove!'
/app/vendor/bundle/ruby/2.1.0/gems/carrierwave-0.5.8/lib/carrierwave/uploader/callbacks.rb:17:in `with_callbacks'
/app/vendor/bundle/ruby/2.1.0/gems/carrierwave-0.5.8/lib/carrierwave/uploader/remove.rb:14:in `remove!'
/app/vendor/bundle/ruby/2.1.0/gems/carrierwave-0.5.8/lib/carrierwave/mount.rb:365:in `remove!'
/app/vendor/bundle/ruby/2.1.0/gems/carrierwave-0.5.8/lib/carrierwave/mount.rb:205:in `remove_avatar!'
(irb):41:in `irb_binding'
/app/vendor/ruby-2.1.0/lib/ruby/2.1.0/irb/workspace.rb:86:in `eval'
/app/vendor/ruby-2.1.0/lib/ruby/2.1.0/irb/workspace.rb:86:in `evaluate'
/app/vendor/ruby-2.1.0/lib/ruby/2.1.0/irb/context.rb:380:in `evaluate'
/app/vendor/ruby-2.1.0/lib/ruby/2.1.0/irb.rb:492:in `block (2 levels) in eval_input'
/app/vendor/ruby-2.1.0/lib/ruby/2.1.0/irb.rb:624:in `signal_status'
/app/vendor/ruby-2.1.0/lib/ruby/2.1.0/irb.rb:489:in `block in eval_input'
/app/vendor/ruby-2.1.0/lib/ruby/2.1.0/irb/ruby-lex.rb:247:in `block (2 levels) in each_top_level_statement'
/app/vendor/ruby-2.1.0/lib/ruby/2.1.0/irb/ruby-lex.rb:233:in `loop'
/app/vendor/ruby-2.1.0/lib/ruby/2.1.0/irb/ruby-lex.rb:233:in `block in each_top_level_statement'
/app/vendor/ruby-2.1.0/lib/ruby/2.1.0/irb/ruby-lex.rb:232:in `catch'
/app/vendor/ruby-2.1.0/lib/ruby/2.1.0/irb/ruby-lex.rb:232:in `each_top_level_statement'
/app/vendor/ruby-2.1.0/lib/ruby/2.1.0/irb.rb:488:in `eval_input'
/app/vendor/ruby-2.1.0/lib/ruby/2.1.0/irb.rb:397:in `block in start'
/app/vendor/ruby-2.1.0/lib/ruby/2.1.0/irb.rb:396:in `catch'
/app/vendor/ruby-2.1.0/lib/ruby/2.1.0/irb.rb:396:in `start'
/app/vendor/bundle/ruby/2.1.0/gems/railties-3.2.18/lib/rails/commands/console.rb:47:in `start'
/app/vendor/bundle/ruby/2.1.0/gems/railties-3.2.18/lib/rails/commands/console.rb:8:in `start'
/app/vendor/bundle/ruby/2.1.0/gems/railties-3.2.18/lib/rails/commands.rb:41:in `<top (required)>'
script/rails:6:in `require'
script/rails:6:in `<main>'
=> [:remove_versions!]

我也在Carrierwave的Github跟踪器上打开了一个问题。

1 个答案:

答案 0 :(得分:0)

我打算解决这个问题,因为我们可以解决这个问题。

我忘了提到我们也在使用carrierwave-mongoid。我认为这拯救了我们的屁股。

通过手动设置实例头像然后重新保存模型并忽略Excon错误,我们强制模型使用其默认图像。

所以在这种情况下,假设我们有一个名为t的模型的实例。

> t[:avatar] = nil
> t.save rescue nil

这改变了avatar.url以解析为默认图像。