Paperclip 是一个很棒的 Rails 上传插件。将上传内容存储在本地文件系统或 Amazon S3 上似乎效果很好。我只是假设将文件存储在本地主机上,但是这个应用程序需要使用 S3,因为它将托管在 Heroku 上。
我将如何在一次压缩下载中从 S3 获取所有上传/附件?
从本地文件系统获取文件的 zip 文件似乎很简单。它从 S3 获取文件让我感到困惑。我认为这可能与 rubyzip 处理 URL 引用的文件的方式有关。我尝试了各种方法,但似乎无法避免错误。
format.zip {
registrations_with_attachments = Registration.find_by_sql('SELECT * FROM registrations WHERE abstract_file_name NOT LIKE ""')
headers['Cache-Control'] = 'no-cache'
tmp_filename = "#{RAILS_ROOT}/tmp/tmp_zip_" <<
Time.now.to_f.to_s <<
".zip"
# rubyzip gem version 0.9.1
# rdoc http://rubyzip.sourceforge.net/
Zip::ZipFile.open(tmp_filename, Zip::ZipFile::CREATE) do |zip|
#get all of the attachments
# attempt to get files stored on S3
# FAIL
registrations_with_attachments.each { |e| zip.add("abstracts/#{e.abstract.original_filename}", e.abstract.url(:original, false)) }
# => No such file or directory - http://s3.amazonaws.com/bucket/original/abstract.txt
# Should note that these files in S3 bucket are publicly accessible. No ACL.
# works with local storage. Thanks to Henrik Nyh
# registrations_with_attachments.each { |e| zip.add("abstracts/#{e.abstract.original_filename}", e.abstract.path(:original)) }
end
send_data(File.open(tmp_filename, "rb+").read, :type => 'application/zip', :disposition => 'attachment', :filename => tmp_filename.to_s)
File.delete tmp_filename
}