红宝石 - 在现有的s3的文件的末尾使用雾追加内容 [英] Ruby - Append content at the end of the existing s3 file using fog

查看:117
本文介绍了红宝石 - 在现有的s3的文件的末尾使用雾追加内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在S3现有的或新创建的文件追加文本。我使用和我有以下code

 要求'雾'
文件=abc.csv
斗='my_bucket
存储=雾:: Storage.new(:供应商=>AWS,:aws_access_key_id =>XXXXXXXX,:aws_secret_access_key =>'YYYYYYYY)
DIR = connection.directories.new(:关键=>斗)#没有坏处,如果桶已经存在,如果不创建一个
缓冲= [big_chunk1,big_chunk2,big_chunk3,big_chunk4,big_chunk5]

#我需要这行后帮助。无变化以上。
buffer.each做|块|
  #这是行不通的,因为它不会追加现有的文件下方的文字或
  #新创建的。我不是在寻找解决方案建议,buffer.join('')
  #然后写整个块一次。我必须写在卡盘的一些具体原因。
  #我也不想先读取现有数据,然后采取内存
  #再追加,最后写回。
  dir.files.create(:关键=>文件:身体=>大块,:市民=>假)
结束
 

解决方案

在上传到S3,文件是不可变的 - 它不能更改或添加到

如果你只是希望上传块的文件,您可以使用多部分上传API,(假设你有在10000块,而除了最后将至少5MB),但是你会可能需要(如您当前不是使用雾机型)下降到雾请求级别。

How to append text in an existing or newly created file in S3. I am using fog and I have following code

require 'fog'
file    = "abc.csv"
bucket  = 'my_bucket'
storage = Fog::Storage.new(:provider => 'AWS', :aws_access_key_id => 'XXXXXXXX', :aws_secret_access_key => 'YYYYYYYY')
dir     = connection.directories.new(:key => bucket) # no harm, if this bucket already exists, if not create one
buffer  = ["big_chunk1", "big_chunk2", "big_chunk3", "big_chunk4", "big_chunk5"]

# I need help after this line. No changes above.
buffer.each do |chunk|
  # this will not work as it will not append text below the existing file or 
  # newly created one. I am not looking for solution suggesting, buffer.join('') 
  # and then write whole chunk at once. I have to write in chuck for some specific reason.
  # Also I dont want to first read existing data and then take in to memory
  # and then append and finally write back.
  dir.files.create(:key => file, :body => chunk, :public => false) 
end

解决方案

Once uploaded to S3, a file is immutable - it cannot be changed or appended to.

If you a just looking to upload a file in chunks, you may be able to use the multipart upload api, (assuming you have under 10000 chunks and that all but the last will be at least 5MB), however you'd probably need to drop to the fog request level (instead of using the fog models as you are currently).

这篇关于红宝石 - 在现有的s3的文件的末尾使用雾追加内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆