简体   繁体   English

Rubyzip:直接将zip文件导出到S3而不将tmpfile写入磁盘?

[英]Rubyzip: Export zip file directly to S3 without writing tmpfile to disk?

I have this code, which writes a zip file to disk, reads it back, uploads to s3, then deletes the file: 我有这个代码,它将一个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)

This code works already but what I want is to not create the zip file anymore, to save a few steps. 这段代码已经可以使用,但我想要的是不再创建zip文件 ,以节省一些步骤。 I was wondering if there is a way to export the zipfile data directly to s3 without having to first create a tmpfile, read it back, then delete it? 我想知道是否有办法将zipfile数据直接导出到s3而不必先创建一个tmp文件,读回来然后删除它?

I think I just found the answer to my question. 我想我刚刚找到了问题的答案。

It's Zip::ZipOutputStream.write_buffer . 它是Zip :: ZipOutputStream.write_buffer I'll check this out and update this answer when I get it working. 当我开始工作时,我会检查出来并更新这个答案。

Update 更新

It does work. 它确实有效。 My code is like this now: 我的代码现在是这样的:

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')

The write_buffer returns a StringIO and needs to rewind the stream first before read ing it. write_buffer返回一个StringIO ,需要在read之前先回滚该流。 Now I don't need to create and delete the tmpfile. 现在我不需要创建和删除tmpfile。

I'm just wondering now if write_buffer would be more memory extensive or heavier than open ? 我现在只是想知道write_buffer是否会比open更大或更重? Or is it the other way around? 或者是周围的其他方式?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM