Search code examples
c#file-uploadmicrosoft-graph-apionedrivemicrosoft-graph-sdks

How to improve speed when uploading large files using OneDrive SDK


When uploading large file attachments using OneDrive SDK, the speed is too slow.

It takes about 45 minutes to 1 hour based on 1GB file.

Is there any way to improve the problem?

public async Task<JObject> UploadLargeFiles(string upn, IFormFile files)
    {
        var jObject = new JObject();
        int fileSize = Convert.ToInt32(files.Length);
        var fileStream = files.OpenReadStream();

        GraphServiceClient client = await MicrosoftGraphClient.GetGraphServiceClient();

        var uploadProps = new DriveItemUploadableProperties
        {
            ODataType = null,
            AdditionalData = new Dictionary<string, object>
            {
                { "@microsoft.graph.conflictBehavior", "rename" }
            }
        };

        var item = this.SelectUploadFolderID(upn).Result;
        var uploadSession = await client.Users[upn].Drive.Items[item].ItemWithPath(files.FileName).CreateUploadSession(uploadProps).Request().PostAsync();

        int maxChunkSize = 320 * 1024;
        var uploadTask = new LargeFileUploadTask<DriveItem>(uploadSession, fileStream, maxChunkSize);

        var response = await uploadTask.UploadAsync();

        if (response.UploadSucceeded)
        {
            jObject = JObject.FromObject(new { result = "success"});
            return jObject;
        }
        else
        {
            return null;
        }
    }

Solution

  • I have followed the examples/guidance's provided at blogpost and it worked for me. I know uploading bigger files may take a while, give a fact that it depends on the internet speed, so i will exclude Graph API out of the picture here. Apart from that i dont see any options to speedup. Also you can consider filing uservoice.