首页 > 解决方案 > ActiveStorage 从 S3 服务上的附件对象下载文件/IO?

问题描述

文件上传后,我想分析并立即处理。

我目前正在附加然后处理每个:

current_account.archives.attach(archive_params)
current_account.archives.each do |archive|
  Job.enqueue(AccountArchiveImportJob.new(current_account.id, archive.id))
end

在工作中,我正在打开 CSV 并解析垃圾

attachment = Account.find(account_id).archives.where(id: archive_id).first

CSV.parse(attachment.download) do |row|
  do_stuff_with_the_row(row)
end

我想做类似的事情:

CSV.foreach(attachment.open) do |row|
  do_stuff_with_the_row(row)
end

我找不到允许将附件转换回 FILE 的文档

标签: ruby-on-railsrails-activestorage

解决方案


至少从 Rails 6.0 rc1 开始:

model.attachment_changes['attachment_name'].attachable

将在上传之前为您提供原始 TmpFile 的IO


推荐阅读