Rails Paperclip 上传成功删除本地附件

Rails Paperclip delete local attachment if upload successful

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

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))

既然你知道文件的路径,你就可以在它上传到 s3 后做这样的事情 -

def remove_file
  File.delete(@filename) if File.exist?(@filename)
end