An easy way to hook up various file storage mechanisms, following the repository pattern with a factory.
This is available on NuGet - just click one of the buttons above, or search for Creek.FileRepository
in Visual Studio.
To utilise a repository you'll need to set up the relevant section in your appsettings.json
, for example this section for an SFTP repository:
"SftpRepository": {
"Host": "my-server.local",
"Port": 22,
"Username": "usernamegoeshere",
"Password": "passwordgoeshere",
"RemoteDirectoryPath": "/home/username/foldername"
}
You can load this into your standard config in a dotnet core app, or manually load it with something like:
private static IConfiguration InitConfiguration()
{
IConfigurationRoot configBuilder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
return configBuilder;
}
Use the enum of RepositoryTypes to select the repository you want, then use the factory to instantiate it.
Factory.RepositoryType repositoryType = Factory.RepositoryType.Sftp;
IFileRepository fileRepository = Factory.GetFileRepository(repositoryType, config);
RepoFile repoFile = new RepoFile();
// .. Make changes to a Stream contentStream property here
repoFile = repoFile.GenerateFile(filename, contentStream);
await fileRepository.CreateFileAsync(repoFile);
RepoFile repoFile = await fileRepository.ReadFileAsync(filename);
// .. Make changes to the file's Content property here
await fileRepository.UpdateFileAsync(repoFile);
await fileRepository.DeleteFileAsync(filename);
The following storage mechanisms are included:
- Disk [in progress]
- MongoDB [in progress]
- S3 [in progress]
- SFTP
- SQL [in progress]
The appsettings sections for each one that isn't in progress
are included below.
"SftpRepository": {
"Host": "",
"Port": 22,
"Username": "",
"Password": "",
"RemoteDirectoryPath": "/"
}