Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Drive API Upload Format Invalid?

I've been using the following method to upload images to Google Drive:

    public string AddFile(string path, string contentType, string driveId)
    {
        FilesResource.CreateMediaUpload request;
        using (FileStream stream = new FileStream(path,
            FileMode.Open))
        {
            Google.Apis.Drive.v3.Data.File fileMetadata = new Google.Apis.Drive.v3.Data.File
            {
                Name = Path.GetFileName(path),
                DriveId = driveId
            };
            request = _service.Files.Create(
                fileMetadata, stream, contentType);
            request.Fields = "id";
            request.SupportsTeamDrives = true;
        }
        IUploadProgress requestResult = request.Upload();
        if (requestResult.Exception != null) throw requestResult.Exception;
        Google.Apis.Drive.v3.Data.File file = request.ResponseBody;
        return file == null ? "" : file.Id;
    }

It was working fine until today, using the API client library version 1.40.3.1694. Now I get this sort of error in requestResult.Exception:

System.FormatException: The format of value 'bytes 0--1/660915' is invalid.

The file I'm uploading is just a small GIF test file that gets deleted right away in this case.

Am I missing something obvious?

like image 607
Riddari Avatar asked Feb 06 '26 10:02

Riddari


1 Answers

I understand:

  • Why the original code is broken
  • Why using a MemoryStream fixes it
  • How to fix it more simply

I don't yet understand:

  • The exact error you're seeing (I'll dig into that separately; I've filed an issue for it)
  • How your code has ever actually worked

The problem is that you're opening a FileStream, creating a request but not executing it, disposing of the FileStream and then executing the request. In your MemoryStream version, you don't dispose of the stream until the upload is complete.

Creating the initial request doesn't actually read the data from the stream - it just prepares things. You need to keep your stream open until Upload has completed. So all you need to do is make the using statement bigger:

public string AddFile(string path, string contentType, string driveId)
{
    using (FileStream stream = new FileStream(path, FileMode.Open))
    {
        Google.Apis.Drive.v3.Data.File fileMetadata = new Google.Apis.Drive.v3.Data.File
        {
            Name = Path.GetFileName(path),
            DriveId = driveId
        };
        var request = _service.Files.Create(fileMetadata, stream, contentType);
        request.Fields = "id";
        request.SupportsTeamDrives = true;
        IUploadProgress requestResult = request.Upload();
        if (requestResult.Exception != null) throw requestResult.Exception;
        Google.Apis.Drive.v3.Data.File file = request.ResponseBody;
        return file == null ? "" : file.Id;
    }
}
like image 66
Jon Skeet Avatar answered Feb 07 '26 23:02

Jon Skeet