红宝石 - 使用雾在现有S3文件的末尾附加内容(Ruby - Append content at t

2019-08-08 05:06发布

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

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

Answer 1:

一旦上传到S3,文件是不可变的 - 它不能被改变或追加到。

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



文章来源: Ruby - Append content at the end of the existing s3 file using fog