我有一个小型的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错误。非常感谢任何帮助。可能并不是很难,只是我做错了。