ruby-on-rails - 压缩存储在 S3 上的所有回形针附件

标签 ruby-on-rails ruby zip amazon-s3 paperclip

Paperclip 是一个很棒的 Rails 上传插件。在本地文件系统或 Amazon S3 上存储上传似乎效果很好。我假设将文件存储在本地主机上,但此应用需要使用 S3,因为它将托管在 Heroku 上。

我如何在一次压缩下载中从 S3 获取所有上传/附件?

本地文件系统 中获取文件的 zip 似乎很简单。它从 S3 获取文件让我感到困惑。我认为这可能与 ruby​​zip 处理 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(...)

参见:

更新

来自changelog :

New in 3.0.1:

关于ruby-on-rails - 压缩存储在 S3 上的所有回形针附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2338758/

相关文章:

ruby-on-rails - 如何读取包含双引号 (") 的 CSV

mysql - 创建通知数据库表的最佳实践

使用脚本语言的 C++ 单元测试

java - 从ZIP文件中获取字节[]

linux - 从管道在命令行上创建 zip 时,如何在 zip 中指定文件名?

ruby-on-rails - 参数在哪里[:id] come from in rails?

ruby-on-rails - Rails 4 erb Ruby 什么都不返回

ruby - 使用 Thin 和 EventMachine 运行模块化 Sinatra 应用程序。启动两次?

ruby-on-rails - 使用 Ruby 的 Tumblr API

python - 如何使用 python 检索 zip 文件中的目录?