gpt4 book ai didi

ruby-on-rails-3 - rubyZip gem : Want to zip remote files in RoR

转载 作者:行者123 更新时间:2023-12-04 23:06:26 26 4
gpt4 key购买 nike

我想在压缩后从我的网站下载照片。我正在使用 ruby​​Zip 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
文档.url = http://myApplication.s3.amazonaws.com/xxxxxxxx/image

现在在这里我收到以下错误:

没有这样的文件或目录 - myApplication.s3.amazonaws.com/xxxxxxxx/image

如果我从本地文件系统(例如:/home/user/images)压缩文件但不适用于远程文件,则此 gem 工作正常。

我做错了什么吗?有人可以帮我吗?或者任何其他可以做到这一点的 gem ?

谢谢,
-塔尼雅特

最佳答案

您可以做的是首先从 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

关于ruby-on-rails-3 - rubyZip gem : Want to zip remote files in RoR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11169275/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com