tah*_*yat 4 rubyzip zipfile ruby-on-rails-3
我想在压缩后从我的网站下载照片.我使用rubyZip gem但无法压缩远程文件.以下是场景:
我正在尝试从服务器压缩内容.内容就是这样的
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.amazonaws.com/xxxxxxxx/image
如果我从本地文件系统(例如:/ home/user/images)压缩文件而不是远程文件压缩文件,这个gem工作正常.
我做错了吗?有人可以帮我吗?或者任何其他可以做到这一点的宝石?
谢谢,-Tahniyat
您可以做的是首先从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
Run Code Online (Sandbox Code Playgroud)
参考:http: //rubyzip.sourceforge.net/classes/Zip/ZipOutputStream.html
| 归档时间: |
|
| 查看次数: |
2954 次 |
| 最近记录: |