Skip to content

A library to provide an interface for common file services with implementations

License

Notifications You must be signed in to change notification settings

gaepdit/file-service

Repository files navigation

Georgia EPD-IT File Service Library

This library was created by Georgia EPD-IT to provide common file services for our web applications.

Georgia EPD-IT .NET Test SonarCloud Quality Gate Status Lines of Code

Installation

Nuget

To install, search for "GaEpd.FileService" in the NuGet package manager or run the following command:

dotnet add package GaEpd.FileService

Usage

An IFileService interface is used to abstract out common file persistence operations:

  • SaveFileAsync
  • FileExistsAsync
  • GetFilesAsync
  • GetFileAsync
  • TryGetFileAsync
  • DeleteFileAsync

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 InMemory is chosen, all other settings are ignored.

  • If FileSystem is chosen, then FileSystemBasePath is required, and NetworkUsername, NetworkDomain, and NetworkPassword can be provided if needed. Other settings are ignored.

  • If AzureBlobStorage is chosen, then AzureAccountName and BlobContainer are required, and BlobBasePath and AzureTenantId can be provided if needed. Other settings are ignored.

In Memory

The in-memory file service implementation stores files in memory.

builder.Services.AddSingleton<IFileService, InMemoryFileService>();

File System

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));

Azure Blob Storage

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));

Azure Blob Storage Authentication

This library uses the DefaultAzureCredential class to authenticate with Azure Blob Storage. Review the documentation on using developer accounts during local development.

About

A library to provide an interface for common file services with implementations

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Contributors 2

  •  
  •  

Languages