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
platformIPlatformAdapterhttpClientHttpClientloggerILogger
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
fileRemoteFileThe file to monitor for the state transition.
maxSecondsintThe maximum time, in seconds, to wait for the file to become active.
cancellationTokenCancellationTokenA 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
namestringThe name of the remote file to be deleted.
cancellationTokenCancellationTokenA 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
fileRemoteFileThe file to download. It must have a DownloadUri.
destinationStreamA writable stream that receives the file content.
cancellationTokenCancellationTokenA 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
fileRemoteFileThe file to download. It must have a DownloadUri.
cancellationTokenCancellationTokenA token to monitor for cancellation requests.
Returns
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
namestringThe file name (
files/{id}or{id}) or its download/URI URL.destinationStreamA writable stream that receives the file content.
cancellationTokenCancellationTokenA 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
namestringThe file name (
files/{id}or{id}) or its download/URI URL.cancellationTokenCancellationTokenA token to monitor for cancellation requests.
Returns
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
namestringThe file name (
files/{id}or{id}) or its download/URI URL.destinationPathstringThe local file path to write to (created or overwritten).
cancellationTokenCancellationTokenA 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
namestringThe name of the RemoteFile to get.
cancellationTokenCancellationTokenToken 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
pageSizeint?The maximum number of RemoteFiles to return per request. Defaults to 10 if not specified, with a maximum limit of 100.
pageTokenstringAn optional token for retrieving the next page of RemoteFiles from a previous call.
cancellationTokenCancellationTokenA 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
urisIEnumerable<string>The Cloud Storage URIs to register, e.g.
gs://bucket/object.accessTokenstringAn 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.
quotaProjectIdstringOptional quota project, sent as the
x-goog-user-projectheader.cancellationTokenCancellationTokenA 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
filePathstringThe path to the file to upload.
progressCallbackAction<double>An optional callback to report the upload progress.
cancellationTokenCancellationTokenAn 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
streamStreamThe file stream to be uploaded.
displayNamestringThe display name for the uploaded file.
mimeTypestringThe MIME type of the file to be uploaded.
progressCallbackAction<double>An optional callback function to monitor the upload progress, with the progress represented as a percentage.
cancellationTokenCancellationTokenA token used to propagate notifications that the operation should be canceled.
Returns
- Task<RemoteFile>
A RemoteFile object representing the metadata of the uploaded file.