简体   繁体   English

Paperclip - 从Amazon S3删除文件?

[英]Paperclip - delete a file from Amazon S3?

I need to be able to delete files from S3 that are stored by users, such as profile photos. 我需要能够从S3中删除用户存储的文件,例如个人资料照片。 Just calling @user.logo.destroy doesn't seem to do the trick - I get [paperclip] Saving attachments. 只是调用@user.logo.destroy似乎没有办法 - 我得到[paperclip] Saving attachments. in the logs and the file stays right there in the S3 bucket. 在日志中,文件保留在S3存储桶中。

How can the file itself be removed? 如何删除文件本身?

This are the methods from Paperclip that can be used to remove the attachments: 这是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

So you see, destroy only removes the attachment if no error occurs. 所以你看,如果没有错误发生,destroy只删除附件。 I have tried it with my own setup against S3 so I know that destroy works. 我已经尝试了我自己的设置对S3,所以我知道毁灭工作。

Could the problem in your case possible be that you have any validations that cancels the save? 您的案例中的问题可能是您有任何取消保存的验证吗? Ie validates_attachment_presence or something similar? 即validates_attachment_presence或类似的东西?

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. 我认为找到的一种方法是尝试@ user.logo.destroy,然后检查@ user.errors的内容,看它是否报告任何错误消息。

This seems like an answer to your question, although I don't totally understand their distinction between destroy and clear (I don't know which model has_attached_file, page or image): 这似乎是你的问题的答案,虽然我不完全理解他们在destroy和clear之间的区别(我不知道哪个模型has_attached_file,page或image):

Rails Paperclip how to delete attachment? Rails Paperclip如何删除附件?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM