Class ResumableUpload (1.60.0)

public abstract class ResumableUpload

Media upload which uses Google's resumable media upload protocol to upload data.

Inheritance

object > ResumableUpload

Namespace

GoogleApisUpload

Assembly

Google.Apis.dll

Remarks

See: https://developers.google.com/drive/manage-uploads#resumable for more information on the protocol.

Constructors

ResumableUpload(Stream, ResumableUploadOptions)

protected ResumableUpload(Stream contentStream, ResumableUploadOptions options)

Creates a ResumableUpload instance.

Parameters
Name Description
contentStream Stream

The data to be uploaded. Must not be null.

options ResumableUploadOptions

The options for the upload operation. May be null.

Fields

chunkSize

protected int chunkSize

Change this value ONLY for testing purposes!

Field Value
Type Description
int

DefaultChunkSize

public const int DefaultChunkSize = 10485760

Default chunk size. Default value is 10*MB.

Field Value
Type Description
int

MinimumChunkSize

public const int MinimumChunkSize = 262144

Minimum chunk size (except the last one). Default value is 256*KB.

Field Value
Type Description
int

Properties

ChunkSize

public int ChunkSize { get; set; }

Gets or sets the size of each chunk sent to the server. Chunks (except the last chunk) must be a multiple of MinimumChunkSize to be compatible with Google upload servers.

Property Value
Type Description
int

ContentStream

public Stream ContentStream { get; }

Gets or sets the stream to upload.

Property Value
Type Description
Stream

Options

protected ResumableUploadOptions Options { get; }

Gets the options used to control the resumable upload.

Property Value
Type Description
ResumableUploadOptions

UploadStreamInterceptor

public StreamInterceptor UploadStreamInterceptor { get; set; }

Interceptor used to propagate data successfully uploaded on each chunk.

Property Value
Type Description
StreamInterceptor

Methods

CreateFromUploadUri(Uri, Stream, ResumableUploadOptions)

public static ResumableUpload CreateFromUploadUri(Uri uploadUri, Stream contentStream, ResumableUploadOptions options = null)

Creates a ResumableUpload instance for a resumable upload session which has already been initiated.

Parameters
Name Description
uploadUri Uri

The session URI of the resumable upload session. Must not be null.

contentStream Stream

The data to be uploaded. Must not be null.

options ResumableUploadOptions

The options for the upload operation. May be null.

Returns
Type Description
ResumableUpload

The instance which can be used to upload the specified content.

Remarks

See https://cloud.google.com/storage/docs/json_api/v1/how-tos/resumable-upload#start-resumable for more information about initiating resumable upload sessions and saving the session URI, or upload URI.

ExceptionForResponseAsync(HttpResponseMessage)

protected Task<GoogleApiException> ExceptionForResponseAsync(HttpResponseMessage response)

Creates a GoogleApiException instance using the error response from the server.

Parameter
Name Description
response HttpResponseMessage

The error response.

Returns
Type Description
TaskGoogleApiException

An exception which can be thrown by the caller.

GetProgress()

public IUploadProgress GetProgress()

Get the current progress state.

Returns
Type Description
IUploadProgress

An IUploadProgress describing the current progress of the upload.

See Also

InitiateSessionAsync(CancellationToken)

public abstract Task<Uri> InitiateSessionAsync(CancellationToken cancellationToken = default)

Initiates the resumable upload session and returns the session URI, or upload URI. See https://developers.google.com/drive/manage-uploads#start-resumable and https://cloud.google.com/storage/docs/json_api/v1/how-tos/resumable-upload#start-resumable for more information.

Parameter
Name Description
cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns
Type Description
TaskUri

The task containing the session URI to use for the resumable upload.

ProcessResponse(HttpResponseMessage)

protected virtual void ProcessResponse(HttpResponseMessage httpResponse)

Process a response from the final upload chunk call.

Parameter
Name Description
httpResponse HttpResponseMessage

The response body from the final uploaded chunk.

Resume()

public IUploadProgress Resume()

Resumes the upload from the last point it was interrupted. Use when resuming and the program was not restarted.

Returns
Type Description
IUploadProgress

Resume(Uri)

public IUploadProgress Resume(Uri uploadUri)

Resumes the upload from the last point it was interrupted. Use when the program was restarted and you wish to resume the upload that was in progress when the program was halted. Implemented only for ContentStreams where .CanSeek is True.

Parameter
Name Description
uploadUri Uri

VideosResource.InsertMediaUpload UploadUri property value that was saved to persistent storage during a prior execution.

Returns
Type Description
IUploadProgress
Remarks

In your application's UploadSessionData Event Handler, store UploadUri.AbsoluteUri property value (resumable session URI string value) to persistent storage for use with Resume() or ResumeAsync() upon a program restart. It is strongly recommended that the FullPathFilename of the media file that is being uploaded is saved also so that a subsequent execution of the program can compare the saved FullPathFilename value to the FullPathFilename of the media file that it has opened for uploading. You do not need to seek to restart point in the ContentStream file.

ResumeAsync()

public Task<IUploadProgress> ResumeAsync()

Asynchronously resumes the upload from the last point it was interrupted.

Returns
Type Description
TaskIUploadProgress
Remarks

You do not need to seek to restart point in the ContentStream file.

ResumeAsync(CancellationToken)

public Task<IUploadProgress> ResumeAsync(CancellationToken cancellationToken)

Asynchronously resumes the upload from the last point it was interrupted. Use when resuming and the program was not restarted.

Parameter
Name Description
cancellationToken CancellationToken

A cancellation token to cancel the asynchronous operation.

Returns
Type Description
TaskIUploadProgress
Remarks

You do not need to seek to restart point in the ContentStream file.

ResumeAsync(Uri)

public Task<IUploadProgress> ResumeAsync(Uri uploadUri)

Asynchronously resumes the upload from the last point it was interrupted. Use when resuming and the program was restarted. Implemented only for ContentStreams where .CanSeek is True.

Parameter
Name Description
uploadUri Uri

VideosResource.InsertMediaUpload UploadUri property value that was saved to persistent storage during a prior execution.

Returns
Type Description
TaskIUploadProgress
Remarks

In your application's UploadSessionData Event Handler, store UploadUri.AbsoluteUri property value (resumable session URI string value) to persistent storage for use with Resume() or ResumeAsync() upon a program restart. It is strongly recommended that the FullPathFilename of the media file that is being uploaded is saved also so that a subsequent execution of the program can compare the saved FullPathFilename value to the FullPathFilename of the media file that it has opened for uploading. You do not need to seek to restart point in the ContentStream file.

ResumeAsync(Uri, CancellationToken)

public Task<IUploadProgress> ResumeAsync(Uri uploadUri, CancellationToken cancellationToken)

Asynchronously resumes the upload from the last point it was interrupted. Use when the program was restarted and you wish to resume the upload that was in progress when the program was halted. Implemented only for ContentStreams where .CanSeek is True.

Parameters
Name Description
uploadUri Uri

VideosResource.InsertMediaUpload UploadUri property value that was saved to persistent storage during a prior execution.

cancellationToken CancellationToken

A cancellation token to cancel the asynchronous operation.

Returns
Type Description
TaskIUploadProgress
Remarks

In your application's UploadSessionData Event Handler, store UploadUri.AbsoluteUri property value (resumable session URI string value) to persistent storage for use with Resume() or ResumeAsync() upon a program restart. It is strongly recommended that the FullPathFilename of the media file that is being uploaded is saved also so that a subsequent execution of the program can compare the saved FullPathFilename value to the FullPathFilename of the media file that it has opened for uploading. You do not need to seek to restart point in the ContentStream file.

SendNextChunkAsync(Stream, CancellationToken)

protected Task<bool> SendNextChunkAsync(Stream stream, CancellationToken cancellationToken)

Uploads the next chunk of data to the server.

Parameters
Name Description
stream Stream
cancellationToken CancellationToken
Returns
Type Description
Taskbool

True if the entire media has been completely uploaded.

Upload()

public IUploadProgress Upload()

Uploads the content to the server. This method is synchronous and will block until the upload is completed.

Returns
Type Description
IUploadProgress
Remarks

In case the upload fails the Exception will contain the exception that cause the failure.

UploadAsync()

public Task<IUploadProgress> UploadAsync()

Uploads the content asynchronously to the server.

Returns
Type Description
TaskIUploadProgress

UploadAsync(CancellationToken)

public Task<IUploadProgress> UploadAsync(CancellationToken cancellationToken)

Uploads the content to the server using the given cancellation token.

Parameter
Name Description
cancellationToken CancellationToken

A cancellation token to cancel operation.

Returns
Type Description
TaskIUploadProgress
Remarks

In case the upload fails Exception will contain the exception that cause the failure. The only exception which will be thrown is TaskCanceledException which indicates that the task was canceled.

Events

ProgressChanged

public event Action<IUploadProgress> ProgressChanged

Event called whenever the progress of the upload changes.

Event Type
Type Description
ActionIUploadProgress

UploadSessionData

public event Action<IUploadSessionData> UploadSessionData

Event called when an UploadUri is created. Not needed if the application program will not support resuming after a program restart.

Event Type
Type Description
ActionIUploadSessionData
Remarks

Within the event, persist the UploadUri to storage. It is strongly recommended that the full path filename (or other media identifier) is also stored so that it can be compared to the current open filename (media) upon restart.

Extension Method