ruby-on-rails - 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 的文档
解决方案
至少从 Rails 6.0 rc1 开始:
model.attachment_changes['attachment_name'].attachable
将在上传之前为您提供原始 TmpFile 的IO。
推荐阅读
- powershell - 如何使用 .bat 脚本从非静态链接下载文件
- mysql - 局部变量上的MySQL海象运算符?
- c# - 将对象数组发布到 ASP.NET MVC 控制器得到 403 错误
- sql-server - 在存储过程中插入 SQL WHERE 子句作为参数,并仅使用非空子句及其参数值
- javascript - 在使用 JSON.parse 之前应该如何清理不受信任的 JSON?
- javascript - 我的 javascript 没有显示错误,但它没有做任何事情
- mysql - 当我尝试将 mysql 与 Node.js 连接时,我的 docker-compose 文件不起作用
- python - 使用moviepy包(Python)的问题
- javascript - 如何使用 Array.reduce() 在给定的 JavaScript 代码中解析 Promise?
- rest - API 应该如何处理带有错误 FK Id 的正文的 POST 请求?