Skip to content

File upload fails with ODataError: Invalid request #2867

@syncimprov

Description

@syncimprov

Describe the bug

I have a simple app to upload files form a specific folder to a specific OneDrive folder. This app has been working without issues for over 18 months. Since mid-February, it stopped working with this error:

Invalid Request.

Here my code snippet:

public static async Task SyncFileOld(string driveId, SyncItem syncItem)
     {
         try
         {
             // https://learn.microsoft.com/en-us/graph/sdks/large-file-upload?tabs=csharp
             var driveUpload = new DriveUpload.CreateUploadSessionPostRequestBody()
             {
                 Item = new DriveItemUploadableProperties
                 {
                     Name = Path.GetFileName(syncItem.Path),
                     AdditionalData = new Dictionary<string, object>
                     {
                         { "@microsoft.graph.conflictBehavior", "replace" },
                     },
                     FileSystemInfo = new Microsoft.Graph.Models.FileSystemInfo()
                     {
                         LastModifiedDateTime = File.GetLastWriteTimeUtc(syncItem.Path)
                     },
                     Description = "desc"
                 }
             };

             // note: this does not always return the correct info
             var response = await _graphServiceClient!
                 .Drives[driveId]
                 .Items[syncItem.FolderId]
                 .SearchWithQ(q: driveUpload.Item.Name)
                 .GetAsSearchWithQGetResponseAsync();

             if (response?.Value?.Count > 0)
             {
                 logger.Warn($"File {driveUpload.Item.Name} already exists. Skipping.");
                 return;
             }

             
// ==> this line throws the exception
var uploadSession = await _graphServiceClient!
                 .Drives[driveId]
                 .Items[syncItem.FolderId]
                 .ItemWithPath(driveUpload.Item.Name)
                 .CreateUploadSession
                 .PostAsync(driveUpload);


             logger.Trace($"Uploading {Path.GetFileName(syncItem.Path)} ...");

             using (var fileStream = File.OpenRead(syncItem.Path))
             {
                 int maxSliceSize = 320 * 1024 * 10;
                 var fileUploadTask = new LargeFileUploadTask<DriveItem>(
                     uploadSession, fileStream, maxSliceSize, _graphServiceClient.RequestAdapter);

                 var totalLength = fileStream.Length;
                 // Create a callback that is invoked after each slice is uploaded
                 IProgress<long> progress = new Progress<long>(prog =>
                 {
                     logger.Trace($"Uploaded {prog} bytes of {totalLength} bytes.");
                 });

                 var uploadResult = await fileUploadTask.UploadAsync(progress);

                 if (uploadResult.UploadSucceeded)
                 {
                     logger.Trace($"Uploaded {Path.GetFileName(syncItem.Path)} successfully.");
                 }
                 else
                 {
                     logger.Error($"Error in uploading {Path.GetFileName(syncItem.Path)}.");
                 }
             }

         }
         catch (Exception ex)
         {
             logger.Error(ex, $"Unable to upload file {syncItem.Path}.");
         }
     }

Variations: If I remove AdditionalData, I get a different error: 401 (access denied? not sure why). Nothing has changed in the app registration/permissions. I created a new app registration and still got the same error.

Expected behavior

The upload should work as expected.

How to reproduce

SDK Version

5.73.0

Latest version known to work for scenario above?

No response

Known Workarounds

None, to my knowledge.

Debug output

Click to expand log ```
</details>


### Configuration

- OS: Windows (11)
- .NET core 9.0

### Other information

_No response_

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions