Table of Contents

Class FileClient

Namespace
GenerativeAI.Clients
Assembly
GenerativeAI.dll

A client for interacting with the Gemini API Files endpoint.

public class FileClient : BaseClient
Inheritance
FileClient
Inherited Members
Extension Methods

Constructors

FileClient(IPlatformAdapter, HttpClient?, ILogger?)

A client for interacting with the Gemini API Files endpoint.

public FileClient(IPlatformAdapter platform, HttpClient? httpClient = null, ILogger? logger = null)

Parameters

platform IPlatformAdapter
httpClient HttpClient
logger ILogger

Remarks

Provides methods for uploading, retrieving, listing, and deleting files, as well as awaiting file state changes.

Methods

AwaitForFileStateActiveAsync(RemoteFile, int, CancellationToken)

Awaits until a file reaches the "ACTIVE" state or times out after the specified duration.

public Task AwaitForFileStateActiveAsync(RemoteFile file, int maxSeconds, CancellationToken cancellationToken)

Parameters

file RemoteFile

The file to monitor for the state transition.

maxSeconds int

The maximum time, in seconds, to wait for the file to become active.

cancellationToken CancellationToken

A token to cancel the waiting operation.

Returns

Task

An awaitable task that completes when the file reaches the "ACTIVE" state.

Exceptions

GenerativeAIException

Thrown when the file encounters a processing error.

DeleteFileAsync(string, CancellationToken)

Deletes a remote file by its name asynchronously.

public Task DeleteFileAsync(string name, CancellationToken cancellationToken = default)

Parameters

name string

The name of the remote file to be deleted.

cancellationToken CancellationToken

A token to monitor for cancellation requests.

Returns

Task

A task that represents the asynchronous delete operation.

DownloadFileAsync(RemoteFile, Stream, CancellationToken)

Streams the content of a generated file to destination. Gemini Developer API only.

public Task DownloadFileAsync(RemoteFile file, Stream destination, CancellationToken cancellationToken = default)

Parameters

file RemoteFile

The file to download. It must have a DownloadUri.

destination Stream

A writable stream that receives the file content.

cancellationToken CancellationToken

A token to monitor for cancellation requests.

Returns

Task

A task that completes when the content has been written.

Exceptions

ArgumentException

The file has no download URI (uploaded files can't be downloaded).

DownloadFileAsync(RemoteFile, CancellationToken)

Downloads the content of a generated file into memory. Gemini Developer API only.

public Task<byte[]> DownloadFileAsync(RemoteFile file, CancellationToken cancellationToken = default)

Parameters

file RemoteFile

The file to download. It must have a DownloadUri.

cancellationToken CancellationToken

A token to monitor for cancellation requests.

Returns

Task<byte[]>

The file content.

Exceptions

ArgumentException

The file has no download URI (uploaded files can't be downloaded).

DownloadFileAsync(string, Stream, CancellationToken)

Streams the content of a generated file to destination without buffering the whole file in memory. Gemini Developer API only.

public Task DownloadFileAsync(string name, Stream destination, CancellationToken cancellationToken = default)

Parameters

name string

The file name (files/{id} or {id}) or its download/URI URL.

destination Stream

A writable stream that receives the file content.

cancellationToken CancellationToken

A token to monitor for cancellation requests.

Returns

Task

A task that completes when the content has been written.

Exceptions

NotSupportedException

Called on Vertex AI.

DownloadFileAsync(string, CancellationToken)

Downloads the content of a generated file (for example a generated video) into memory, like files.download of the google-genai SDK. Gemini Developer API only.

public Task<byte[]> DownloadFileAsync(string name, CancellationToken cancellationToken = default)

Parameters

name string

The file name (files/{id} or {id}) or its download/URI URL.

cancellationToken CancellationToken

A token to monitor for cancellation requests.

Returns

Task<byte[]>

The file content.

Remarks

Files created by upload can't be downloaded; check DownloadUri or Source.

Exceptions

NotSupportedException

Called on Vertex AI.

DownloadFileToPathAsync(string, string, CancellationToken)

Downloads the content of a generated file to a local file path. Gemini Developer API only.

public Task DownloadFileToPathAsync(string name, string destinationPath, CancellationToken cancellationToken = default)

Parameters

name string

The file name (files/{id} or {id}) or its download/URI URL.

destinationPath string

The local file path to write to (created or overwritten).

cancellationToken CancellationToken

A token to monitor for cancellation requests.

Returns

Task

A task that completes when the file has been written.

GetFileAsync(string, CancellationToken)

Gets the metadata for the given RemoteFile.

public Task<RemoteFile> GetFileAsync(string name, CancellationToken cancellationToken = default)

Parameters

name string

The name of the RemoteFile to get.

cancellationToken CancellationToken

Token to monitor for cancellation requests.

Returns

Task<RemoteFile>

The RemoteFile information.

See Also

ListFilesAsync(int?, string?, CancellationToken)

Asynchronously retrieves a list of metadata for RemoteFiles owned by the requesting project.

public Task<ListFilesResponse> ListFilesAsync(int? pageSize = null, string? pageToken = null, CancellationToken cancellationToken = default)

Parameters

pageSize int?

The maximum number of RemoteFiles to return per request. Defaults to 10 if not specified, with a maximum limit of 100.

pageToken string

An optional token for retrieving the next page of RemoteFiles from a previous call.

cancellationToken CancellationToken

A CancellationToken to observe while waiting for the task to complete.

Returns

Task<ListFilesResponse>

A task that represents the asynchronous operation. The result contains a ListFilesResponse object with the list of RemoteFiles and associated metadata.

RegisterFilesAsync(IEnumerable<string>, string?, string?, CancellationToken)

Registers Google Cloud Storage files with the Files service so they can be used in requests, like files.register_files of the google-genai SDK. Gemini Developer API only.

public Task<RegisterFilesResponse> RegisterFilesAsync(IEnumerable<string> uris, string? accessToken = null, string? quotaProjectId = null, CancellationToken cancellationToken = default)

Parameters

uris IEnumerable<string>

The Cloud Storage URIs to register, e.g. gs://bucket/object.

accessToken string

An OAuth 2.0 access token with read access to the objects. When omitted, the platform's access token (or its authenticator / Application Default Credentials) is used.

quotaProjectId string

Optional quota project, sent as the x-goog-user-project header.

cancellationToken CancellationToken

A token to monitor for cancellation requests.

Returns

Task<RegisterFilesResponse>

The registered files.

Exceptions

NotSupportedException

Called on Vertex AI.

UploadFileAsync(string, Action<double>?, CancellationToken)

Uploads a file to the remote server and creates a RemoteFile object.

public Task<RemoteFile> UploadFileAsync(string filePath, Action<double>? progressCallback = null, CancellationToken cancellationToken = default)

Parameters

filePath string

The path to the file to upload.

progressCallback Action<double>

An optional callback to report the upload progress.

cancellationToken CancellationToken

An optional token to cancel the upload operation.

Returns

Task<RemoteFile>

The uploaded file's information as a RemoteFile object.

UploadStreamAsync(Stream, string, string, Action<double>?, CancellationToken)

Asynchronously uploads a file stream to the remote server and returns the uploaded file's metadata.

public Task<RemoteFile> UploadStreamAsync(Stream stream, string displayName, string mimeType, Action<double>? progressCallback = null, CancellationToken cancellationToken = default)

Parameters

stream Stream

The file stream to be uploaded.

displayName string

The display name for the uploaded file.

mimeType string

The MIME type of the file to be uploaded.

progressCallback Action<double>

An optional callback function to monitor the upload progress, with the progress represented as a percentage.

cancellationToken CancellationToken

A token used to propagate notifications that the operation should be canceled.

Returns

Task<RemoteFile>

A RemoteFile object representing the metadata of the uploaded file.

See Also