回形针 - 删除Amazon S3的文件? [英] Paperclip - delete a file from Amazon S3?

查看:815
本文介绍了回形针 - 删除Amazon S3的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够从由用户,如个人资料照片存储S3删除文件。只需调用 @ user.logo.destroy 似乎并没有这样的伎俩 - 我得到 [回形针]保存附件在日志和文件保持正确的,在S3的桶中。

I need to be able to delete files from S3 that are stored by users, such as profile photos. Just calling @user.logo.destroy doesn't seem to do the trick - I get [paperclip] Saving attachments. in the logs and the file stays right there in the S3 bucket.

如何在文件本身被删除?

How can the file itself be removed?

推荐答案

此是从回形针,可用于除去附着物的方法:

This are the methods from Paperclip that can be used to remove the attachments:

module Paperclip
  class Attachment
    # 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
      queue_existing_for_delete
      @errors            = {}
    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

所以你看,破坏只删除附件,如果没有发生错误。我试图用我自己的设置对S3,所以我知道,破坏作品。

So you see, destroy only removes the attachment if no error occurs. I have tried it with my own setup against S3 so I know that destroy works.

难道这个问题,你的情况可能是,你有什么验证能够消除保存?即validates_attachment_ presence或类似的东西?

Could the problem in your case possible be that you have any validations that cancels the save? I.e validates_attachment_presence or something similar?

我想找出是尝试@ user.logo.destroy,然后检查@ user.errors的内容,看它是否报告任何错误信息的一种方式。

I think one way to find out would be to try @user.logo.destroy and then check the content of @user.errors to see if it reports any error messages.

这篇关于回形针 - 删除Amazon S3的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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