This library was created by Georgia EPD-IT to provide common file services for our web applications.
To install, search for "GaEpd.FileService" in the NuGet package manager or run the following command:
dotnet add package GaEpd.FileService
An IFileService interface is used to abstract out common file persistence operations:
SaveFileAsyncFileExistsAsyncGetFilesAsyncGetFileAsyncTryGetFileAsyncDeleteFileAsync
The library also includes three useful implementations, In Memory, File System, and Azure Blob Storage. Each implementation can be registered independently as shown in the sections below.
Alternatively, the AddFileServices extension method can be used to automatically register an implementation based on
the configuration. To do so, register the file services configuration as follows:
builder.AddFileServices();And add the following section to your configuration:
{
"FileServiceSettings": {
"FileService": "",
"FileSystemBasePath": "",
"NetworkUsername": "",
"NetworkDomain": "",
"NetworkPassword": "",
"AzureAccountName": "",
"AzureTenantId": "",
"BlobContainer": "",
"BlobBasePath": ""
}
}The FileService setting must be set to InMemory, FileSystem, or AzureBlobStorage.
-
If
InMemoryis chosen, all other settings are ignored. -
If
FileSystemis chosen, thenFileSystemBasePathis required, andNetworkUsername,NetworkDomain, andNetworkPasswordcan be provided if needed. Other settings are ignored. -
If
AzureBlobStorageis chosen, thenAzureAccountNameandBlobContainerare required, andBlobBasePathandAzureTenantIdcan be provided if needed. Other settings are ignored.
The in-memory file service implementation stores files in memory.
builder.Services.AddSingleton<IFileService, InMemoryFileService>();The file system service writes files to a local or network drive. The basePath parameter is required and defines where
the files will be stored. If basePath doesn't exist, it will be created.
builder.Services.AddTransient<IFileService, FileSystemFileService>(_ =>
new FileSystemFileService(basePath));If a Windows Identity is required to access the desired file location, use the overload that
accepts username, domain, and password parameters in the constructor.
builder.Services.AddTransient<IFileService, FileSystemFileService>(_ =>
new FileSystemFileService(basePath, username, domain, password));The Azure Blob Storage service requires an Azure account and an existing Blob Storage container. (The service does not
attempt to create the container if it does not exist.) The basePath parameter is optional and is prepended to file
names as a path segment.
The tenantId parameter is also optional and specifies the ID of the tenant to which the
credential will authenticate by default. (This is useful in multi-tenant situations where the desired Tenant ID is not
the default.)
builder.Services.AddSingleton<IFileService, AzureBlobFileService>(_ =>
new AzureBlobFileService(accountName, container, basePath, tenantId));This library uses the
DefaultAzureCredential
class to authenticate with Azure Blob Storage. Review the documentation
on using developer accounts during local development.
