简体   繁体   中英

Rails 5 - stream file in smaller chunks?

I am sending a file from my rails server to a microcontroller. The microcontroller is running out of memory because (we believe) the file is being sent in chunks that are too large - up to 16 kb at a time.

How can I take the StringIO object I have from S3 and send it to the requestor in 4kb chunks?

My current implementation:

file_name = "#{version}.zip"
firmware_file = s3(file_name).get()
response.headers['Content-Length'] = firmware_file.body.size.to_s
send_data firmware_file.body.read, filename: file_name

Rails has ActionController::Live module which helps you stream response in real time. In your case, as you want to create smaller chunks and send it to client (microcontroller), this feature might be useful.

"File System Monitoring" section of the article Is It Live?by aaron patterson explains how change in file system can be pushed to client in real time with ActionController::Live .

Hope this helps!

In short, you need to exploit ActionController::Live in order to stream response data to your client(s).

Since you are transferring zipfiles you can use the elegantly simple zipline gem. What I particularly like about this gem is that it supports a large number of streamable object types - so just about anything you can think of to throw at it, it will figure out how to stream it without much effort on your part.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM