压缩存储在S3上的所有Paperclip附件

cha*_*erx 15 ruby zip ruby-on-rails amazon-s3 paperclip

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
          }
Run Code Online (Sandbox Code Playgroud)

vla*_*adr 11

你几乎肯定想用e.abstract.to_file.path而不是e.abstract.url(...).

看到:

UPDATE

来自更改日志:

3.0.1中的新功能: