GridFS Resumable Upload

Based on my reading of the MongoDB.Driver code for .NET, when writing a file using GridFS any partial writes for that file will be deleted if the upload is aborted.

I have a mobile app that needs to upload images, often high quality and therefore large, that is regularly used in situations with poor connectivity.

My thinking was to use GridFS to handle those uploads. But there doesn’t seem to be a way to resume an upload that has been interrupted which is critical for this app.

Have I missed something?

riser

Hi, Robert,

You are correct that GridFSBucket.UploadFromStream will delete partial uploads when it fails to read from the stream. This is by design to ensure that partial uploads are cleaned up as Stream objects do not support resume out of the box.

You could use GridFSBucket.OpenUploadStream, which would then give you full control to call Abort (which will delete incomplete uploads) or Close (which will write the metadata). You’ll have to investigate how you could use this partially complete upload and append to it.

Another possibility would be to implement your own resume functionality where you write into an in-memory buffer - resume the upload as needed - and then using GridFSBucket.UploadFromBytes to upload the finished buffer.

A third possibility would be to combine these techniques to resume failed uploads using some scratch database and recombine all the partial uploads in an in-memory buffer, which is then written to your final storage collection via GridFSBucket.UploadFromBytes.

Hopefully this gives you some ideas for how to solve your problem.

Sincerely,
James

1 Like