2012-06-23 22 views
4

我想要在壓縮後從我的網站上下載照片。我正在使用rubyZip gem,但無法壓縮遠程文件。以下是方案:ruby​​Zip寶石:想要在RoR中壓縮遠程文件

我想從服務器壓縮內容。內容是這樣的

http://myApplication.s3.amazonaws.com/xxxxxxxx/image/image1.jpeg

所以在 「zipfile.add(attachment.document_file_name,attachment.document.url)」,我分配下列值:

document_file_name = image1.jpeg/IMAGE2 .JPEG/image3.jpeg document.url = http://myApplication.s3.amazonaws.com/xxxxxxxx/image

現在,這裏我得到以下錯誤:

沒有這樣的文件或目錄 - myApplication.s3.a mazonaws.com/xxxxxxxx/image

如果我從本地文件系統(例如:/ home/user/images)壓縮文件,但不能用於遠程文件,此gem工作正常。

我做錯了什麼?有人可以幫我嗎?或者其他可以做到這一點的寶石?

感謝, -Tahniyat

+0

看到這個庫:https://github.com/fringd/zipline – iwasrobbed

回答

8

你所能做的就是讀它首先從S3其直接寫入到存檔文件(放存檔文件到臨時目錄),服務則刪除臨時存檔文件。這裏有一個小片斷:

require 'zip/zip' 

    s3 = Aws::S3.new(S3_KEY, S3_SECRET) 
    bucket_gen = Aws::S3Generator::Bucket.create(s3, S3_BUCKET) 
    archive_file = "#{Rails.root}/tmp/archive.zip" 

    Zip::ZipOutputStream.open(archive_file) do |zos| 
    list_of_files_to_loop.each do |file| 
     filename = file.filename 
     url = "#{S3_PATH}/#{filename}" 

     signed_url = bucket_gen.get(URI.unescape(URI.parse(URI.escape(url)).path[1..-1]), 1.minute) 

     zos.put_next_entry(filename) # Give it next file a filename 
     zos.print(URI.parse(signed_url).read) # Add file to zip 
    end 
    end # Write zip file 

    # TODO: Serve file 
    # TODO: Delete archived file from tmp directory 

參考: http://rubyzip.sourceforge.net/classes/Zip/ZipOutputStream.html

+2

對於這個新郵編語法爲: '郵編::的OutputStream .open(...)' –

+0

而你只需要'require'zip'',而不是'require'zip/zip''。請參閱http://stackoverflow.com/questions/5997539/using-rubyzip-error-no-such-file-to-load-zip-zip – talyric

+0

如果這不起作用,請添加此gem:'zip-zip'。適用於我。 – Abel