0

Does anyone know how to use the C# OneDrive SDK to perform a resumable upload?

When I use IDriveItemRequestBuilder.CreateUploadSession I always get a new session with the NextExpectedRanges reset. If I use the .UploadURL and manually send a HTTP Post I get the correct, next ranges back however I don't then know the means to resume the upload session using the sdk. There doesn't seem to be a means from the API to 'OpenUploadSession', or at least that I can find. Nor can I find a working example. I suspect this must be a common use case.

Please note that keywords in the text - resumable.

nearproc
  • 35
  • 8
  • FYI, not ideal but caching UploadSessions (merging the last NextExpectedRanges) seems to work but its far from idea and wont assist with resuming after a process restart. – nearproc Jun 17 '19 at 17:02
  • Possible duplicate of [How to upload a large document in c# using the Microsoft Graph API rest calls](https://stackoverflow.com/questions/49776955/how-to-upload-a-large-document-in-c-sharp-using-the-microsoft-graph-api-rest-cal) – Brad Jun 18 '19 at 19:46
  • 1
    How is this possibly a duplicate? The link you posted shows how to use the API to upload all under one session. As clearly stated in the title, I'm attempting to be able to resume uploads. Which means either re-constructing the upload session from parameters (which appears impossible given that there is no 'open upload session API' or caching the upload session in some sort of map. The latter works but its not ideal; if the process dies then so does the map. Hence if an upload is interrupted due the process termination then the entire file has to be re-uploaded. – nearproc Jun 27 '19 at 12:54
  • My apologies - internally we refer to session based uploads as resumable uploads and I read your question differently. I see what you're asking for now. – Brad Jun 27 '19 at 15:26

2 Answers2

2

I was looking for the same thing and just stepped on an example from the official docs: https://learn.microsoft.com/en-us/graph/sdks/large-file-upload?tabs=csharp.

I tried the code and it worked.

In case, my sample implementation: https://github.com/xiaomi7732/onedrive-sample-apibrowser-dotnet/blob/6639444d6298492c38f841e411066635760930c2/OneDriveApiBrowser/FormBrowser.cs#L565

Saar
  • 199
  • 1
  • 8
0

The method of resumption depends on how much state you have. The absolution minimum that is required is UploadSession.UploadUrl (think of it as unique identifier for the session). If you don't have that URL you'd need to create a new upload session and start from the beginning, otherwise if you do have it you can do something like the following to resume:

var uploadSession = new UploadSession
{
    NextExpectedRanges = Enumerable.Empty<string>(),
    UploadUrl = persistedUploadUrl,
};

var maxChunkSize = 320 * 1024; // 320 KB - Change this to your chunk size. 5MB is the default.
var provider = new ChunkedUploadProvider(uploadSession, graphClient, ms, maxChunkSize);

// This will query the service and make sure the remaining ranges are accurate.
uploadSession = await provider.UpdateSessionStatusAsync();

// Since the remaining ranges is now accurate, this will return the requests required to
// complete the upload.
var chunkRequests = provider.GetUploadChunkRequests();

...

If you have more state you'd be able to skip some of the above. For example, if you already had a ChunkedUploadProvider but don't know that it's accurate (maybe it was serialized to disk or something) then you can just start the process with the call to UpdateSessionStatusAsync.

FYI, you can see the code for ChunkedUploadProvider here in case that'll be helpful to see what's going on under the covers.

Brad
  • 4,089
  • 2
  • 16
  • 26