Paperclip - 从Amazon S3删除文件?

ssc*_*rus 5 ruby-on-rails amazon-s3 paperclip ruby-on-rails-3

我需要能够从S3中删除用户存储的文件,例如个人资料照片.只是打电话@user.logo.destroy似乎没有做到这一点 - 我进入[paperclip] Saving attachments.日志并且文件保留在S3存储桶中.

如何删除文件本身?

Dan*_*nne 3

以下是 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
Run Code Online (Sandbox Code Playgroud)

所以你看,如果没有发生错误,销毁只会删除附件。我已经用我自己的设置对 S3 进行了尝试,所以我知道 destroy 是有效的。

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

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