Upload Media

  • Several API methods support uploading media in addition to a regular body, overloading the regular request method to get an additionalStream to upload.

  • For anyStream you wish to upload, you should use resumable media upload, which allows streams to be uploaded in smaller chunks, especially useful for large files to handle potential network interruptions or failures.

  • Resumable Media Upload has been a feature in the Google API .NET client library since 1.2.0-beta, with convenience methods in API-specific libraries for interaction.

  • The media content is uploaded in chunks with a default size of 10MB, which can be changed to any multiple of 256KB by setting theChunkSize property, and exponential backoff is used for server errors.

  • Make sure the position of the stream you upload is 0 to avoid errors.

Several API methods support uploading media in addition to a regular body. In that case, the regular request method is overloaded to get an additionalStream to upload.

Overview

For anyStream you wish to upload, you should use resumable media upload, which allows streams to be uploaded in smaller chunks. This is especially useful if you are transferring large files, and the likelihood of a network interruption or some other transmission failure is high. It can also reduce your bandwidth usage in the event of network failures because you don't have to restart large file uploads from the beginning.

ResumableMediaUpload

Resumable Media Upload has been a feature in the Google API .NET client library since 1.2.0-beta. The Google API-specific libraries contain convenience methods for interacting with this feature.

The resumable media upload protocol is described, for example, on themedia upload page for the Drive API. The main class of interest isResumableUpload. In this implementation, the media content is uploaded in chunks.

The default chunk size is 10MB, but you can change it by setting theChunkSize property on the request to any multiple of 256KB. If a server error is encountered in a request then exponential backoff policy is used to resend the bytes that were not successfully uploaded. By default, exponential backoff is enabled for each client request. You can change the default behavior when you construct a new service object by changing theDefaultExponentialBackOffPolicy property onBaseClientService.Initializer and/or setting theHttpClientInitializer property to your own implementation ofIConfigurableHttpClientInitializer that adds some backoff policy.

The methods that support media upload are identified in the reference documentation for the API-specific documentation. For these API methods, convenienceUpload andUploadAsync methods are added. Those methods take aStream to upload and its content type as parameters.

Make sure that the position of the stream you upload is 0, otherwise you will receive an error, such as "System.InvalidOperationException: The given header was not found".

Note that due to the behavior of the framework'sHttpClient class, if the upload times out, aTaskCanceledException is thrown. If you see this exception, consider manually increasing theTimeout property in the client used by your service object.

Sample Code

//Createtheserviceusingtheclientcredentials.varservice=newDriveService(newBaseClientService.Initializer(){HttpClientInitializer=credential,ApplicationName="Application_Name"});usingvaruploadStream=System.IO.File.OpenRead("Local_File_Name");//CreatetheFileresourcetoupload.Google.Apis.Drive.v3.Data.FiledriveFile=newGoogle.Apis.Drive.v3.Data.File{Name="Drive_File_Name"};//Getthemediauploadrequestobject.FilesResource.CreateMediaUploadinsertRequest=service.Files.Create(driveFile,uploadStream,"image/jpeg");//Addhandlerswhichwillbenotifiedonprogresschangesanduploadcompletion.//Notificationofprogresschangedwillbeinvokedwhentheuploadwasstarted,//oneachuploadchunk,andonsuccessorfailure.insertRequest.ProgressChanged+=Upload_ProgressChanged;insertRequest.ResponseReceived+=Upload_ResponseReceived;awaitinsertRequest.UploadAsync();staticvoidUpload_ProgressChanged(IUploadProgressprogress)=>Console.WriteLine(progress.Status+" "+progress.BytesSent);staticvoidUpload_ResponseReceived(Google.Apis.Drive.v3.Data.Filefile)=>Console.WriteLine(file.Name+" was uploaded successfully");

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-09-26 UTC.