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
}
你几乎肯定想使用e.abstract.to_file.path
代替e.abstract.url(...)
.
See:
-
回形针::存储::S3::to_file http://dev.thoughtbot.com/paperclip/classes/Paperclip/Storage/S3.html#M000089(应该返回一个
TempFile
)
- 临时文件::路径 http://ruby-doc.org/stdlib/libdoc/tempfile/rdoc/classes/Tempfile.html#M004422
UPDATE
来自变更日志 https://github.com/thoughtbot/paperclip/blob/master/NEWS:
3.0.1 中的新功能:
- API 变更:
#to_file
已被删除。使用#copy_to_local_file http://rubydoc.info/gems/paperclip/Paperclip/Storage/S3#copy_to_local_file-instance_method方法代替。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)