ruby-on-rails - 如何从 Job 内部将 csv 文件写入 S3?
问题描述
我为我的应用程序的客户提供了一个数据备份系统。我收集所有相关的 csv 文件并压缩它们。完成该 zip 文件后,我将其附在电子邮件中。由于其文件系统,此过程在 heroku 上中断。我认为自从 heroku-16 以来,我们可以写入 app/tmp 目录,并且这个过程可能发生在同一个事务中并且文件会很好,但似乎情况并非如此。我什至似乎都没有将文件写入生产中的 tmp 目录(在 Dev 我是)。
所以,我想做的只是将 csv 文件直接写入 S3,然后压缩这些文件并将 .zip 保存到 S3 ......然后,将该文件作为电子邮件附件拉取。为此,我需要生成 csv 文件并将它们从 ActiveJob 内部写入 S3。我已经使用 S3 作为 ActiveStorage 的一部分,但这个过程不会使用 ActiveStorage。
是否有命令让我手动直接上传到 S3 存储桶。我一直在研究文档等,但看不到我在追求什么。
作业(使用 /tmp)
def perform(company_id, recipient_id)
company = Company.find(company_id)
source_folder = "#{ Rails.root }/tmp"
zipfile_name = "company_#{ company.id }_archive.zip"
zipfile_path = "#{ Rails.root }/tmp/#{ zipfile_name }"
input_filenames = []
# USERS: create a new empty csv file,
# ... then add rows to it
# ... and, add the file name to the list of files array
users_file_name = "#{ company.name.parameterize.underscore }_users_list.csv"
input_filenames << users_file_name
users_csv_file = File.new("#{ Rails.root.join('tmp') }/#{ users_file_name }", 'w')
users_csv_file << company.users.to_csv
users_csv_file.close
...
# gather up the created files and zip them
Zip::File.open(zipfile_path, create: true) do |zipfile|
input_filenames.uniq.each do |filename|
zipfile.add(filename, File.join(source_folder, filename))
end
end
puts "attaching data_export".colorize(:red)
company.data_exports.attach(
io: StringIO.new("#{ Rails.root }/tmp/company_14_#{ Time.current.to_date.to_s }_archive.zip"),
filename: 'company_14_archive.zip',
content_type: 'application/zip'
)
last_id = company.data_exports.last.id
puts "sending mail using company.id: #{ company.id }, recipient_id: #{ recipient_id }, company.data_exports.last.id: #{ last_id }".colorize(:red)
CompanyMailer.mail_data_export(
company.id,
recipient_id,
last_id
)
end
解决方案
您可以在 S3 上上传这样的文件
key = "file_name.zip"
file_path = "tmp/file_name.zip"
new_s3_client = Aws::S3::Resource.new(region: 'eu-west-1', access_key_id: '123', secret_access_key: '456')
new_bucket = new_s3_client.bucket('public')
obj = new_bucket.object(key)
obj.upload_file(file_path)
推荐阅读
- sql - 获取 Max(Amount) 和关联的类型
- reactjs - 在反应 js 中切换显示/隐藏 li 上的“活动”类
- avro - 使用 Kafka binder 重播 Spring Cloud Stream 中的死信队列中的消息
- c++ - 带有可变参数的 std::function 列表如何工作
- c - 尝试通过数组打印用户输入时出现意外输出
- entity-framework - Entity Framework 2.0 仅创建第一个自动表
- build - 我可以以某种方式使用 Haxe 中的宏打印一行代码,并将其添加到统计信息中,用于 FFun 吗?
- reactjs - 如何将函数传递给 React.js 中的变量组件?
- string - 在 Haskell 中将标题转换为 url 兼容模式的最佳方法
- python - 从带有一些重复元素的 CSV 文件中构建和提取多维 Python 字典中的值