5
votes

I want to download photos from my website after zipping. I am using rubyZip gem but unable to zip remote files. Following is the scenario:

I am trying to zip content from server. Content is something like this

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

So in "zipfile.add( attachment.document_file_name, attachment.document.url)", i assigned following values:

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

Now here I am getting following error:

No such file or directory - myApplication.s3.amazonaws.com/xxxxxxxx/image

This gem is working fine if I zipped files from local file system (e.g: /home/user/images) but not for remote files.

Is I am doing something wrong? Can someone help me out? Or any other gem which can do this?

Thanks, -Tahniyat

1
See this library: github.com/fringd/ziplineiwasrobbed

1 Answers

9
votes

What you can do is read it first from s3 write it directly to the archive file (put archive file to your temporary directory), serve it then delete the temp archive file. Here's a little snippet:

  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

Reference: http://rubyzip.sourceforge.net/classes/Zip/ZipOutputStream.html