簡體   English   中英

Paperclip - 從Amazon S3刪除文件?

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

我需要能夠從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的內容,看它是否報告任何錯誤消息。

這似乎是你的問題的答案,雖然我不完全理解他們在destroy和clear之間的區別(我不知道哪個模型has_attached_file,page或image):

Rails Paperclip如何刪除附件?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM