红宝石 - 在现有的s3的文件的末尾使用雾追加内容红宝石、末尾、文件、内容

2023-09-11 09:22:11 作者:我若不猖狂谁替我坚强

如何在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).