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

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

从本地文件系统获取压缩文件似乎很简单.它让我感到困惑的是从S3获取文件.我认为这可能与rubyzip处理由URL引用的文件有关.我已经尝试过各种方法,但似乎不能避免错误.

format.zip {
                registrations_with_attachments = Registration.find_by_sql('@R_772_10288@CT * 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

大佬总结

以上是大佬教程为你收集整理的ruby-on-rails – 将存储在S3上的所有Paperclip附件拉上全部内容,希望文章能够帮你解决ruby-on-rails – 将存储在S3上的所有Paperclip附件拉上所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。