我需要能够从S3中删除用户存储的文件,例如个人资料照片。仅仅调用@user.logo.destroy似乎并不能解决问题-我在日志中得到[paperclip] Saving attachments.,文件就在S3存储桶中。

如何删除文件本身?

最佳答案

这是Paperclip中可用于删除附件的方法:

# Clears out the attachment. Has the same effect as previously assigning
# nil to the attachment. Does NOT save. If you wish to clear AND save,
# use #destroy.
def clear(*styles_to_clear)
  if styles_to_clear.any?
    queue_some_for_delete(*styles_to_clear)
  else
    queue_all_for_delete
    @queued_for_write  = {}
    @errors            = {}
  end
end

# Destroys the attachment. Has the same effect as previously assigning
# nil to the attachment *and saving*. This is permanent. If you wish to
# wipe out the existing attachment but not save, use #clear.
def destroy
  clear
  save
end

因此,您可以看到,destroy仅在没有错误发生的情况下才删除附件。我已经用自己的设置针对S3进行了尝试,所以我知道销毁工作正常。

您遇到的问题是否可能是您有任何取消保存的验证?即validates_attachment_presence或类似的东西?

我认为找出答案的一种方法是尝试@ user.logo.destroy,然后检查@ user.errors的内容,看看它是否报告任何错误消息。

关于ruby-on-rails - 回形针-从Amazon S3删除文件吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4450530/

10-12 20:11