ruby-on-rails – 将存储在S3上的所有Paperclip附件拉上

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 将存储在S3上的所有Paperclip附件拉上前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Paperclip是Rails的一个很棒的上传插件.将上传文件存储在本地文件系统或Amazon S3上似乎工作正常.我只想在本地主机上存储文件,但是这个应用程序需要使用S3,因为它将被托管在Heroku上.

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

从本地文件系统获取压缩文件似乎很简单.它让我感到困惑的是从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(…).

看到:

> Paperclip::Storage::S3::to_file(应该返回一个TempFile)
> TempFile::path

UPDATE

changelog

New in 3.0.1:

  • API CHANGE: #to_file has been removed. Use the 07003 method instead.
原文链接:https://www.f2er.com/ruby/273404.html

猜你在找的Ruby相关文章