gpt4 book ai didi

ruby-on-rails - Rubyzip:将 zip 文件直接导出到 S3 而无需将 tmpfile 写入磁盘?

转载 作者:数据小太阳 更新时间:2023-10-29 06:53:50 25 4
gpt4 key购买 nike

我有这段代码,它将一个 zip 文件写入磁盘,读回,上传到 s3,然后删除文件:

compressed_file = some_temp_path

Zip::ZipOutputStream.open(compressed_file) do |zos|
some_file_list.each do |file|
zos.put_next_entry(file.some_title)
zos.print IO.read(file.path)
end
end # Write zip file

s3 = Aws::S3.new(S3_KEY, S3_SECRET)
bucket = Aws::S3::Bucket.create(s3, S3_BUCKET)
bucket.put("#{BUCKET_PATH}/archive.zip", IO.read(compressed_file), {}, 'authenticated-read')

File.delete(compressed_file)

此代码已经有效,但我想要的是不再创建 zip 文件,以节省几个步骤。我想知道是否有一种方法可以将 zip 文件数据直接导出到 s3,而无需先创建一个 tmp 文件,读回它,然后删除它?

最佳答案

我想我刚刚找到了问题的答案。

Zip::ZipOutputStream.write_buffer .我会检查它并在它工作时更新这个答案。

更新

它确实有效。我的代码现在是这样的:

compressed_filestream = Zip::ZipOutputStream.write_buffer do |zos|
some_file_list.each do |file|
zos.put_next_entry(file.some_title)
zos.print IO.read(file.path)
end
end # Outputs zipfile as StringIO

s3 = Aws::S3.new(S3_KEY, S3_SECRET)
bucket = Aws::S3::Bucket.create(s3, S3_BUCKET)

compressed_filestream.rewind
bucket.put("#{BUCKET_PATH}/archive.zip", compressed_filestream.read, {}, 'authenticated-read')

write_buffer 返回 StringIO需要 rewindreading 之前先读取流。现在我不需要创建和删除 tmp 文件。

我现在想知道 write_buffer 是否会比 open 占用更多内存或更重?还是相反?

关于ruby-on-rails - Rubyzip:将 zip 文件直接导出到 S3 而无需将 tmpfile 写入磁盘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15946890/

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