Method: Mongo::Grid::FSBucket#upload_from_stream
- Defined in:
- lib/mongo/grid/fs_bucket.rb
#upload_from_stream(filename, io, opts = {}) ⇒ BSON::ObjectId
Uploads a user file to a GridFS bucket. Reads the contents of the user file from the source stream and uploads it as chunks in the chunks collection. After all the chunks have been uploaded, it creates a files collection document for the filename in the files collection.
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 |
# File 'lib/mongo/grid/fs_bucket.rb', line 435 def upload_from_stream(filename, io, opts = {}) open_upload_stream(filename, opts) do |stream| begin stream.write(io) # IOError and SystemCallError are for errors reading the io. # Error::SocketError and Error::SocketTimeoutError are for # writing to MongoDB. rescue IOError, SystemCallError, Error::SocketError, Error::SocketTimeoutError begin stream.abort rescue Error::OperationFailure end raise end end.file_id end |