拉链拉上存储在S3上的所有回形针附件 [英] Zip up all Paperclip attachments stored on S3

查看:165
本文介绍了拉链拉上存储在S3上的所有回形针附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

回形针是一个伟大的上传插件导轨。在本地文件系统中存储​​上传或Amazon S3似乎运作良好。我只是认为在本地主机存储文件,但使用S3的需要为这个应用程序,因为它会在Heroku上举行。

我将如何去获得所有从S3我的上传/附件单个压缩下载?

获取从的本地文件系统中的文件的zip 的似乎是直线前进。这是越来越从S3有我不解的文件。我认为它可能有一些做与rubyzip处理由URL引用文件的方式。我已经试过各种方法,但似乎无法避免错误。

  format.zip {
                registrations_with_attachments = Registration.find_by_sql(SELECT * FROM注册WHERE abstract_file_name NOT LIKE,)
                标题[的Cache-Control'] ='无缓存'
                tmp_filename =#{} RAILS_ROOT的/ tmp / tmp_zip_<<
                                Time.now.to_f.to_s<<
                                。拉链

                #rubyzip宝石版本0.9.1
                #RDOC http://rubyzip.sourceforge.net/
                邮编:: ZipFile.open(tmp_filename,邮编::的ZipFile :: CREATE)办| ZIP |
                  #获取所有附件

                  #试图获取存储在S3上的文件
                  # 失败
                  registrations_with_attachments.each {| E | zip.add(文摘/#{e.abstract.original_filename},e.abstract.url(:原来,FALSE))}
                  #=>没有这样的文件或目录 -  http://s3.amazonaws.com/bucket/original/abstract.txt
                  #应注意,S3存储桶,这些文件是向公众开放。没有访问控制列表。

                  #适用于本地存储。由于亨里克NYH
                  #registrations_with_attachments.each {| E | zip.add(文摘/#{e.abstract.original_filename},e.abstract.path(:原创))}
                结束

                SEND_DATA(File.open方法(tmp_filename,RB +)读:类型=>应用程序/压缩,:配置=>附件,:文件名=> tmp_filename.to_s)
                File.delete tmp_filename
          }
 

解决方案

您几乎可以肯定要使用 e.abstract.to_file.path 而不是 e.abstract.url(...)

请参阅:

更新

的changelog

  

新的3.0.1:

     

Paperclip is a great upload plugin for Rails. Storing uploads on the local filesystem or Amazon S3 seems to work well. I'd just assume store files on the localhost, but the use of S3 is required for this app as it will be hosted on Heroku.

How would I go about getting all of my uploads/attachments from S3 in a single zipped download?

Getting a zip of files from the local filesystem seems straight forward. It's getting the files from S3 that has me puzzled. I think it may have something to do with the way that rubyzip handles files referenced by URL. I've tried various approaches but can't seem to avoid errors.

    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
          }

解决方案

You almost certainly want to use e.abstract.to_file.path instead of e.abstract.url(...).

See:

UPDATE

From the changelog:

New in 3.0.1:

这篇关于拉链拉上存储在S3上的所有回形针附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆