2013-01-25 55 views
5

如何在S3中追加現有文件或新創建的文件。我使用fog和我有以下代碼Ruby - 使用霧在現有s3文件的末尾附加內容

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 

回答

5

一旦上傳到S3,文件是不可變的 - 它不能更改或追加。

如果你只是想上傳一個文件塊,你可以使用分段上傳api,(假設你有10000塊以下,除了最後一塊至少5MB) d可能需要降到霧請求級別(而不是像當前那樣使用霧模型)。

+0

謝謝@Frederick Cheung。經過這麼多的研究,我得出了同樣的結論。順便說一下,你知道如何使用霧簽名s3 url嗎?在霧文件中找不到關於它的任何信息。 – JVK

相關問題