Class AzureFileManager
Provides file management operations for Azure Blob Storage. Implements IFileManager using Azure Blob Storage as the underlying storage provider.
public class AzureFileManager : IFileManager
- Inheritance
-
AzureFileManager
- Implements
- Inherited Members
Constructors
AzureFileManager(BlobServiceClient, string)
Initializes a new instance of the AzureFileManager class using a Blob Service client.
public AzureFileManager(BlobServiceClient serviceClient, string repositoryName)
Parameters
serviceClientBlobServiceClientThe Azure Blob Service client.
repositoryNamestringThe name of the blob container to use as the file repository.
Exceptions
- ArgumentNullException
Thrown when
serviceClientis null.- ArgumentException
Thrown when
repositoryNameis null, empty, or whitespace.
AzureFileManager(string, string)
Initializes a new instance of the AzureFileManager class using a connection string.
public AzureFileManager(string connectionString, string repositoryName)
Parameters
connectionStringstringThe Azure Storage connection string.
repositoryNamestringThe name of the blob container to use as the file repository.
Exceptions
- ArgumentNullException
Thrown when
connectionStringis null.- ArgumentException
Thrown when
repositoryNameis null, empty, or whitespace.
Properties
Credencial
Gets the Azure Blob Service client used for storage operations.
protected BlobServiceClient Credencial { get; }
Property Value
Repository
Gets the blob container client for the configured repository. The client is lazily initialized on first access.
protected BlobContainerClient Repository { get; }
Property Value
Methods
AddFileAsync(IFile, string?, CancellationToken)
Adds a single file to the storage repository.
public Task<IFile> AddFileAsync(IFile file, string? rootDir = null, CancellationToken cancellationToken = default)
Parameters
fileIFileThe file to add.
rootDirstringOptional root directory path where the file should be stored.
cancellationTokenCancellationTokenA token to cancel the asynchronous operation.
Returns
- Task<IFile>
A task representing the asynchronous operation, containing the added file with updated metadata.
AddFileAsync(Stream, string, string, CancellationToken)
Uploads a stream directly to storage at the specified path.
public Task<IFile> AddFileAsync(Stream stream, string filePath, string contentType, CancellationToken cancellationToken = default)
Parameters
streamStreamThe stream containing the file content.
filePathstringThe full path where the file should be stored in the repository.
contentTypestringThe MIME type of the file content.
cancellationTokenCancellationTokenA token to cancel the asynchronous operation.
Returns
AddFilesAsync(IFile[], CancellationToken)
Adds multiple files to the storage repository.
public Task<IReadOnlyList<IFile>> AddFilesAsync(IFile[] files, CancellationToken cancellationToken = default)
Parameters
filesIFile[]The array of files to add.
cancellationTokenCancellationTokenA token to cancel the asynchronous operation.
Returns
- Task<IReadOnlyList<IFile>>
A task representing the asynchronous operation, containing the collection of added files with updated metadata.
AddFilesAsync(string, IFile[], CancellationToken)
Adds multiple files to a specified root directory in the storage repository.
public Task<IReadOnlyList<IFile>> AddFilesAsync(string rootDir, IFile[] files, CancellationToken cancellationToken = default)
Parameters
rootDirstringThe root directory path where files should be stored.
filesIFile[]The array of files to add.
cancellationTokenCancellationTokenA token to cancel the asynchronous operation.
Returns
- Task<IReadOnlyList<IFile>>
A task representing the asynchronous operation, containing the collection of added files with updated metadata.
Exceptions
- ArgumentException
Thrown when the files array is empty.
CopyFileAsync(string, string, CancellationToken)
Copies a file to another path in the same repository, replacing what is there.
public Task<IFile> CopyFileAsync(string sourcePath, string targetPath, CancellationToken cancellationToken = default)
Parameters
sourcePathstringThe path of the file to copy.
targetPathstringThe path of the copy.
cancellationTokenCancellationTokenA token to cancel the asynchronous operation.
Returns
Exceptions
- FileNotFoundException
Thrown when
sourcePathdoes not exist.- ArgumentException
Thrown when both paths are the same.
DownloadFileAsync(string, Stream, CancellationToken)
Writes the content of a file to a stream.
public Task<bool> DownloadFileAsync(string filePath, Stream destination, CancellationToken cancellationToken = default)
Parameters
filePathstringThe path of the file in the repository.
destinationStreamThe stream the content is written to. It is left open.
cancellationTokenCancellationTokenA token to cancel the asynchronous operation.
Returns
- Task<bool>
A task representing the asynchronous operation, containing
trueif the file was downloaded, orfalseif it does not exist.
FileExistsAsync(string, CancellationToken)
Checks whether a file exists in the storage repository.
public Task<bool> FileExistsAsync(string id, CancellationToken cancellationToken = default)
Parameters
idstringThe unique identifier of the file.
cancellationTokenCancellationTokenA token to cancel the asynchronous operation.
Returns
- Task<bool>
A task representing the asynchronous operation, containing
trueif the file exists; otherwise,false.
GetFileAsync(string, bool, CancellationToken)
Retrieves a file from the storage repository by its identifier.
public Task<IFile?> GetFileAsync(string id, bool download = false, CancellationToken cancellationToken = default)
Parameters
idstringThe unique identifier of the file.
downloadboolIf
true, downloads the file content; otherwise, returns only metadata.cancellationTokenCancellationTokenA token to cancel the asynchronous operation.
Returns
- Task<IFile>
A task representing the asynchronous operation, containing the file if found; otherwise,
null.
GetFileSignedUri(string, TimeSpan, AccessPermissions)
Gets a signed URI for accessing a specific file with specified permissions.
public Uri GetFileSignedUri(string filePath, TimeSpan validFor, AccessPermissions permissions = AccessPermissions.Read)
Parameters
filePathstringThe path to the file in the repository.
validForTimeSpanThe duration for which the signed URI should be valid.
permissionsAccessPermissionsThe access permissions to grant. Defaults to Read.
Returns
- Uri
A signed URI that can be used to access the file with the specified permissions.
GetFilesAsync(int?, CancellationToken)
Retrieves a paginated list of files from the storage repository.
public Task<IReadOnlyList<IFile>> GetFilesAsync(int? pageSize, CancellationToken cancellationToken = default)
Parameters
pageSizeint?The maximum number of files to retrieve per page. If
null, all files are retrieved.cancellationTokenCancellationTokenA token to cancel the asynchronous operation.
Returns
- Task<IReadOnlyList<IFile>>
A task representing the asynchronous operation, containing the collection of files.
Exceptions
- InvalidOperationException
Thrown when the blob container does not exist.
GetRepositorySignedUri(AccessPermissions)
Gets a signed URI for accessing the storage repository with specified permissions.
public Uri GetRepositorySignedUri(AccessPermissions permissions = AccessPermissions.Read)
Parameters
permissionsAccessPermissionsThe access permissions to grant. Defaults to Read.
Returns
- Uri
A signed URI that can be used to access the repository with the specified permissions.
ListFilesAsync(string?, CancellationToken)
Lists the files whose path starts with a prefix, without downloading them.
public IAsyncEnumerable<FileDescriptor> ListFilesAsync(string? prefix = null, CancellationToken cancellationToken = default)
Parameters
prefixstringThe beginning of the paths to list, such as
invoices/2026/. A repository has no real folders: a folder is the prefix its files share.nullor empty lists every file.cancellationTokenCancellationTokenA token to cancel the asynchronous operation.
Returns
- IAsyncEnumerable<FileDescriptor>
The files found, as the provider returns them, read page by page as the sequence is consumed.
MoveFileAsync(string, string, CancellationToken)
Moves a file to another path in the same repository, which is also how a file is renamed.
public Task<IFile> MoveFileAsync(string sourcePath, string targetPath, CancellationToken cancellationToken = default)
Parameters
sourcePathstringThe path of the file to move.
targetPathstringThe new path of the file. What is there is replaced.
cancellationTokenCancellationTokenA token to cancel the asynchronous operation.
Returns
- Task<IFile>
A task representing the asynchronous operation, containing the metadata of the file at its new path.
Remarks
The file is copied and then the original is removed. If the removal fails the file exists at both paths; it is never lost.
Exceptions
- FileNotFoundException
Thrown when
sourcePathdoes not exist.- ArgumentException
Thrown when both paths are the same.
MoveFilesAsync(string, string, CancellationToken)
Moves every file whose path starts with a prefix to another prefix, keeping the rest of each path — which is how a folder is renamed.
public Task<int> MoveFilesAsync(string sourcePrefix, string targetPrefix, CancellationToken cancellationToken = default)
Parameters
sourcePrefixstringThe beginning of the paths to move, such as
drafts/.targetPrefixstringWhat replaces
sourcePrefixin each path, such asfinal/.cancellationTokenCancellationTokenA token to cancel the asynchronous operation.
Returns
Remarks
Files are moved one at a time. If it stops midway, the files already moved stay at the target and the others at the source; running it again finishes the move.
Exceptions
- ArgumentException
Thrown when a prefix is empty or both are the same.
RemoveFileAsync(string, CancellationToken)
Removes a file from the storage repository.
public Task RemoveFileAsync(string id, CancellationToken cancellationToken = default)
Parameters
idstringThe unique identifier of the file to remove.
cancellationTokenCancellationTokenA token to cancel the asynchronous operation.
Returns
- Task
A task representing the asynchronous operation.
RemoveFilesAsync(string, CancellationToken)
Removes every file whose path starts with a prefix — which is how a folder is removed.
public Task<int> RemoveFilesAsync(string prefix, CancellationToken cancellationToken = default)
Parameters
prefixstringThe beginning of the paths to remove. It cannot be empty: emptying a repository is not done by accident.
cancellationTokenCancellationTokenA token to cancel the asynchronous operation.
Returns
Exceptions
- ArgumentException
Thrown when
prefixis empty.