ruby-on-rails - 回形针 - 从 Amazon S3 中删除文件?

标签 ruby-on-rails amazon-s3 paperclip ruby-on-rails-3

我需要能够从 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/

相关文章:

ruby-on-rails - 带 Spring 锁的 Rails 4 控制台

javascript - Rails 应用程序中的 Vue 生命周期 Hook

ruby-on-rails - Paperclip AV 转码器无法在远程服务器上运行

ruby-on-rails - Rails 嵌套表单中的 JQuery UI 日期选择器

ruby-on-rails - 使用Postgres适配器的Rails应用程序无法激活pg

php - AWS OpsWorks - Linux 驱动器到 S3 存储桶

c# - 无法使用 .Net API 读取 Amazon S3 对象元数据,我做错了什么?

amazon-web-services - 如何从 AWS S3 存储中的 VHD 创建 AMI

ruby-on-rails - 图像字段不显示在事件管理表单中

ruby-on-rails - Rails Amazon S3 如何创建文件上传进度条?