Ruby - add content to the end of an existing s3 file using fog

How to add text to an existing or newly created file in S3. I am using fog and I have the 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 
+5
ruby amazon-s3 fog
source share
1 answer

After downloading to S3, the file is unchanged - it cannot be changed or added to.

If you just want to upload the file to chunks, you can use multi-page api download (assuming you have up to 10,000 chunks and all but the last will be at least 5 MB), however you would probably need to go to the request level fog (instead of using fog models like you do now).

+5
source share

All Articles