OneDrive / onedrive-sdk-csharp

OneDrive SDK for C#! https://dev.onedrive.com
Other
294 stars 143 forks source link

Ability to use PushStreamContent for upload #166

Closed oatsoda closed 8 years ago

oatsoda commented 8 years ago

I'd like to write data to the upload stream for uploading data to OneDrive.

I see from the dependant Microsoft.Graph.BaseRequest that at the moment it just wraps it in a StreamContent object.

Is there any way to workaround this and use the PushStreamContent ?

cdmayer commented 8 years ago

This could possibly be done, but the tricky part is you would need to know the size of the stream at creation time. Check out the documentation for resumable uploads. Specifically, it says

Important: Make sure that the total file size specified in the Content-Range header is the same for all file fragments uploaded. If a fragment declares a different size, the PUT request will fail.

If you know the size at creation time, then sure this would be possible. Try it out! Make a pull request!

If you didn't know the size ahead of time but still wanted to use OneDrive as a stream holder, you could write a class that creates a large-ish upload session (maybe 100MB) which you can write to using PushStreamContent. However, you would need to do some internal accounting to watch how many bytes get written to the file, and when the stream is disposed you would write empty content to the rest of the file (otherwise the upload would be considered incomplete, rendering the file useless). This has other issues, such as: wastes storage in your OneDrive, requires significant cleanup on disposal (what if you only wrote 2MB and had to write 98MB of 0's), somehow marking inside the file the end of the content and beginning of the filler (equivalent of the null pointer in a null-terminated string).

oatsoda commented 8 years ago

Sounds like I have probably underestimated this!

I wouldn't know the size. Essentially I need to get some database output into OneDrive files but they are quite large so I was hoping to stream them through to OneDrive rather than buffer the whole file in memory.

I suppose the resumable upload would still be worth looking at as I could read part of the file in to memory, upload it, then read the next part of output, upload it etc. etc.

cdmayer commented 8 years ago

Yep, sounds like you'll have to get creative. Good luck!