views:

766

answers:

2

I am currently using Net::HTTP in a Ruby script to post files to a website via a multipart form post. It works great for small files, but I frequently have to send very large files using this script, and HTTP#post only seems to accept post data as a String object, which means that the file I'm sending has to be read into memory before anything can be sent. This script is running on a busy production server, so it's unacceptable to gobble up hundreds of megabytes of RAM just to send a file.

Ideally, there'd be a method that could be given a buffer size and an IO object, and would send off buffer-sized chunks of data, reading from the IO object only as required. What would be the best way to make this happen? Did I miss something relevant in Net::HTTP?

Update: Net::HTTP#body_stream(input) looks good, though the documentation is rather... sparse. Anyone able to point me to a good example of this in action?

A: 

Use Net::HTTP#body_stream(input)

Example for multipart post without streaming:

VitalieL
+2  A: 

Actually I managed to upload a file using body_stream. The full source code is here: http://stanislavvitvitskiy.blogspot.com/2008/12/multipart-post-in-ruby.html

I ultimately just dug through the Net/HTTP source code to figure out how to use body_stream, which was a right pain. The modifications I made to the multipart code I posted in response to that other question ended up looking a lot like yours. Shame it isn't built in.
zbrimhall