如果上载成功,则Rails回形针删除本地附件

问题描述 投票:0回答:1

我有下面的代码将本地文件保存到AWS S3。它将我的文件正确上传到S3,但是然后我的本地文件没有被删除。现在,我已经完全填满了磁盘空间。上传后,如何要求回形针删除本地文件?

class JobArtifact < ActiveRecord::Base
  attr_reader :remote_url
  has_attached_file :file, path: 'tmp/:id/:fingerprint.:extension'

  do_not_validate_attachment_file_type :file

  def remote_url=(url)
    self.file = URI.parse(url_value)

    @remote_url = url
  end
end

这样称呼:

@filename = "#{Rails.root}/tmp/values.csv"
JobArtifact.create(file: File.open(@filename))
ruby-on-rails ruby paperclip
1个回答
2
投票

由于您知道文件的路径,因此在将文件上传到s3之后,您可以执行以下操作:]

def remove_file
  File.delete(@filename) if File.exist?(@filename)
end
© www.soinside.com 2019 - 2024. All rights reserved.