【发布时间】:2021-12-23 13:52:16
【问题描述】:
我为我的应用程序的客户提供了一个数据备份系统。我收集所有相关的 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
【问题讨论】:
标签: ruby-on-rails amazon-web-services amazon-s3 rails-activestorage