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:
SaveFileAsyncFileExistsAsyncListFilesAsyncGetFileAsyncTryGetFileAsyncDeleteFileAsync
The library includes three useful File Service implementations that store files in memory, the file system, or Azure Blob Storage. A description of each implementation and its parameters is below.
The simplest way to register one of the implementations is to use the AddFileServices() extension method and configure
the desired implementation in the app configuration. Add the following line to your Program file:
builder.AddFileServices();And add the following section to your appsettings.json configuration file:
{
"FileServiceSettings": {
"FileService": "",
"FileSystemBasePath": "",
"NetworkUsername": "",
"NetworkDomain": "",
"NetworkPassword": "",
"AzureAccountName": "",
"AzureTenantId": "",
"BlobContainer": "",
"BlobBasePath": ""
}
}The FileService setting must be included and must be set to either InMemory, FileSystem, or AzureBlobStorage.
Other settings only need to be included if required by the selected implementation.
-
If
InMemoryis chosen, all other settings are ignored and can be left out. -
If
FileSystemis chosen, thenFileSystemBasePathis required.NetworkUsername,NetworkDomain, andNetworkPasswordcan be provided if needed. Other settings are ignored. -
If
AzureBlobStorageis chosen, thenAzureAccountNameandBlobContainerare required.BlobBasePathandAzureTenantIdcan be provided if needed. Other settings are ignored.
Alternatively, you can directly register one of the implementations as shown in the sections below.
The in-memory implementation stores files in memory. All data will be lost when the app restarts (only useful for development).
builder.Services.AddSingleton<IFileService, InMemory>();The file system implementation 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 in the file system, it will be created.
builder.Services.AddTransient<IFileService, FileSystem>(_ => new FileSystem(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, FileSystem>(_ => new FileSystem(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, AzureBlobStorage>(_ => new AzureBlobStorage(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.
