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
NameDescription
contentStreamStream

The data to be uploaded. Must not be null.

optionsResumableUploadOptions

The options for the upload operation. May be null.

Fields

chunkSize

protected int chunkSize

Change this value ONLY for testing purposes!

Field Value
TypeDescription
int

DefaultChunkSize

public const int DefaultChunkSize = 10485760

Default chunk size. Default value is 10*MB.

Field Value
TypeDescription
int

MinimumChunkSize

public const int MinimumChunkSize = 262144

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

Field Value
TypeDescription
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
TypeDescription
int

ContentStream

public Stream ContentStream { get; }

Gets or sets the stream to upload.

Property Value
TypeDescription
Stream

Options

protected ResumableUploadOptions Options { get; }

Gets the options used to control the resumable upload.

Property Value
TypeDescription
ResumableUploadOptions

UploadStreamInterceptor

public StreamInterceptor UploadStreamInterceptor { get; set; }

Interceptor used to propagate data successfully uploaded on each chunk.

Property Value
TypeDescription
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
NameDescription
uploadUriUri

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

contentStreamStream

The data to be uploaded. Must not be null.

optionsResumableUploadOptions

The options for the upload operation. May be null.

Returns
TypeDescription
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
NameDescription
responseHttpResponseMessage

The error response.

Returns
TypeDescription
TaskGoogleApiException

An exception which can be thrown by the caller.

GetProgress()

public IUploadProgress GetProgress()

Get the current progress state.

Returns
TypeDescription
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
NameDescription
cancellationTokenCancellationToken

The token to monitor for cancellation requests.

Returns
TypeDescription
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
NameDescription
httpResponseHttpResponseMessage

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
TypeDescription
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
NameDescription
uploadUriUri

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

Returns
TypeDescription
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
TypeDescription
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
NameDescription
cancellationTokenCancellationToken

A cancellation token to cancel the asynchronous operation.

Returns
TypeDescription
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
NameDescription
uploadUriUri

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

Returns
TypeDescription
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
NameDescription
uploadUriUri

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

cancellationTokenCancellationToken

A cancellation token to cancel the asynchronous operation.

Returns
TypeDescription
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
NameDescription
streamStream
cancellationTokenCancellationToken
Returns
TypeDescription
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
TypeDescription
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
TypeDescription
TaskIUploadProgress

UploadAsync(CancellationToken)

public Task<IUploadProgress> UploadAsync(CancellationToken cancellationToken)

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

Parameter
NameDescription
cancellationTokenCancellationToken

A cancellation token to cancel operation.

Returns
TypeDescription
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
TypeDescription
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
TypeDescription
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