使用CarrierWave下载并压缩已上传到S3的文件

20

我有一个小型的Rails 3.2.1应用程序,使用CarrierWave 0.5.8上传文件到S3(使用Fog)

我希望用户能够选择他们想要下载的一些图片,然后将它们压缩成一个zip文件并发送给他们。以下是我的实现方案:

def generate_zip
  #A collection of Photo objects. The Photo object has a PhotoUploader mounted.
  photos = Photo.all

  tmp_filename = "#{Rails.root}/tmp/" << Time.now.strftime('%Y-%m-%d-%H%M%S-%N').to_s << ".zip"
  zip = Zip::ZipFile.open(tmp_filename, Zip::ZipFile::CREATE)
  zip.close

  photos.each do |photo|
    file_to_add = photo.photo.file
    zip = Zip::ZipFile.open(tmp_filename)
    zip.add("tmp/", file_to_add.path)
    zip.close
  end

  #do the rest.. like send zip or upload file and e-mail link

end

这不起作用是因为photo.photo.file返回的是CarrierWave::Storage::Fog::File类的实例,而不是常规文件。

编辑:这导致的错误是:

Errno::ENOENT: 没有名为uploads/photos/name.jpg的文件或目录

我还尝试了以下方法:

tmp_filename = "#{Rails.root}/tmp/" << Time.now.strftime('%Y-%m-%d-%H%M%S-%N').to_s << ".zip"
    zip = Zip::ZipFile.open(tmp_filename, Zip::ZipFile::CREATE)
    zip.close

    photos.each do |photo|
      processed_uri = URI.parse(URI.escape(URI.unescape(photo.photo.file.authenticated_url)).gsub("[", "%5B").gsub("]", "%5D"))
      file_to_add = CarrierWave::Uploader::Download::RemoteFile.new(processed_uri)
      zip = Zip::ZipFile.open(tmp_filename)
      zip.add("tmp/", file_to_add.path)
      zip.close
    end

但这让我得到了403错误。非常感谢任何帮助。可能并不是很难,只是我做错了。


你使用了哪个宝石?Rubyzip 宝石吗? - Rafael Oliveira
2个回答

16
我已经在 @ffoeg 的帮助下解决了问题。
@ffoeg 提供的解决方案对于我来说并不完全有效,因为我处理的 zip 文件大小超过 500MB,在 Heroku 上会导致问题。因此,我使用 resque 将压缩移到后台进程中:

app/workers/photo_zipper.rb:

require 'zip/zip'
require 'zip/zipfilesystem'
require 'open-uri'
class PhotoZipper
  @queue = :photozip_queue

  #I pass 
  def self.perform(id_of_object_with_images, id_of_user_to_be_notified)
    user_mail = User.where(:id => id_of_user_to_be_notified).pluck(:email)
    export = PhotoZipper.generate_zip(id_of_object_with_images, id_of_user_to_be_notified)

    Notifications.zip_ready(export.archive_url, user_mail).deliver
  end

    # Zipfile generator
  def self.generate_zip(id_of_object_with_images, id_of_user_to_be_notified)
    object = ObjectWithImages.find(id_of_object_with_images)
    photos = object.images
    # base temp dir
    temp_dir = Dir.mktmpdir
    # path for zip we are about to create, I find that ruby zip needs to write to a real file
    # This assumes the ObjectWithImages object has an attribute title which is a string.
    zip_path = File.join(temp_dir, "#{object.title}_#{Date.today.to_s}.zip")

    Zip::ZipOutputStream.open(zip_path) do |zos|
      photos.each do |photo|
        path = photo.photo.path
        zos.put_next_entry(path)
        zos.write photo.photo.file.read
      end
    end

    #Find the user that made the request
    user = User.find(id_of_user_to_be_notified)

    #Create an export object associated to the user
    export = user.exports.build

    #Associate the created zip to the export
    export.archive = File.open(zip_path)

    #Upload the archive
    export.save!

    #return the export object
    export
  ensure

    # clean up the tempdir now!
    FileUtils.rm_rf temp_dir if temp_dir
  end


end

app/controllers/photos_controller.rb:

  format.zip do
    #pick the last ObjectWithImages.. ofcourse you should include your own logic here
    id_of_object_with_images = ObjectWithImages.last.id

    #enqueue the Photozipper task
    Resque.enqueue(PhotoZipper, id_of_object_with_images, current_user.id)

    #don't keep the user waiting and flash a message with information about what's happening behind the scenes
    redirect_to some_path, :notice => "Your zip is being created, you will receive an e-mail once this process is complete"
  end

感谢@ffoeg的帮助。如果你的压缩文件更小,你可以尝试@ffoeg的解决方案。

嗨,archive是一个名为export的数据库表的列吗?数据类型是什么?二进制的吗?谢谢。 - user1883793

11

这是我的看法。可能会有打字错误,但我认为这就是要点 :)

# action method, stream the zip
def download_photos_as_zip # silly name but you get the idea
  generate_zip do |zipname, zip_path|
    File.open(zip_path, 'rb') do |zf|
      # you may need to set these to get the file to stream (if you care about that)
      # self.last_modified
      # self.etag
      # self.response.headers['Content-Length']
      self.response.headers['Content-Type'] = "application/zip"
      self.response.headers['Content-Disposition'] = "attachment; filename=#{zipname}"
      self.response.body = Enumerator.new do |out| # Enumerator is ruby 1.9
        while !zf.eof? do
          out << zf.read(4096)
        end
      end
    end
  end
end


# Zipfile generator
def generate_zip(&block)
  photos = Photo.all
  # base temp dir
  temp_dir = Dir.mktempdir
  # path for zip we are about to create, I find that ruby zip needs to write to a real file
  zip_path = File.join(temp_dir, 'export.zip')
  Zip::ZipFile::open(zip_path, true) do |zipfile|
    photos.each do |photo|
      zipfile.get_output_stream(photo.photo.identifier) do |io|
        io.write photo.photo.file.read
      end
    end
  end
  # yield the zipfile to the action
  block.call 'export.zip', zip_path
ensure
  # clean up the tempdir now!
  FileUtils.rm_rf temp_dir if temp_dir
end

非常感谢您的回复。我正在使用Ruby 1.9.3-p125,纠正了一些拼写错误后,我遇到了一个错误,即zipfile.open调用了一个私有方法。(private method `open' called for #Zip::ZipFile:0x007f8a4c7e2510) - Gidogeek
嗯,你需要使用 'zip/zipfilesystem' 库对吧?我会继续研究它。 - ffoeg
我的错。我已经找到解决方案了,即将上线。 - ffoeg
ffoeg,'photo.photo.identifier'是什么?它一直返回null,所以我的zip文件总是空的。我正在使用Gidogeek使用的ZipOutputStream。 - Rafael Oliveira
1
Zip::ZipFile::open更改为Zip::File.open对我有用。 - lafeber
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接