Skip to content

Commit

Permalink
S3storage (#15)
Browse files Browse the repository at this point in the history
* s3 storage Implementation
* S3 Storage sample
* fix readme
  • Loading branch information
AMEST authored Apr 7, 2023
1 parent 94d6c77 commit e46f085
Show file tree
Hide file tree
Showing 15 changed files with 637 additions and 107 deletions.
176 changes: 95 additions & 81 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,82 +1,96 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Skidbladnir.Storage.GridFsStorage.Sample",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build-Skidbladnir.Storage.GridFsStorage.Sample",
"program": "${workspaceFolder}/samples/Storage/Skidbladnir.Storage.GridFsStorage.Sample/bin/Debug/netcoreapp3.1/Skidbladnir.Storage.GridFsStorage.Sample.dll",
"args": [],
"cwd": "${workspaceFolder}/samples/Storage/Skidbladnir.Storage.GridFsStorage.Sample",
"stopAtEntry": false,
"console": "internalConsole"
},
{
"name": "Skidbladnir.Storage.LocalFileStorage.Sample",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build-Skidbladnir.Storage.LocalFileStorage.Sample",
"program": "${workspaceFolder}/samples/Storage/Skidbladnir.Storage.LocalFileStorage.Sample/bin/Debug/netcoreapp3.1/Skidbladnir.Storage.LocalFileStorage.Sample.dll",
"args": [],
"cwd": "${workspaceFolder}/samples/Storage/Skidbladnir.Storage.LocalFileStorage.Sample",
"stopAtEntry": false,
"console": "internalConsole"
},
{
"name": "Skidbladnir.Storage.WebDav.Sample",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build-Skidbladnir.Storage.WebDav.Sample",
"program": "${workspaceFolder}/samples/Storage/Skidbladnir.Storage.WebDav.Sample/bin/Debug/netcoreapp3.1/Skidbladnir.Storage.WebDav.Sample.dll",
"args": [],
"cwd": "${workspaceFolder}/samples/Storage/Skidbladnir.Storage.WebDav.Sample",
"stopAtEntry": false,
"console": "internalConsole"
},
{
"name": "Skidbkadnir.Repository.EntityFrameworkCore.Sample",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build-Skidbkadnir.Repository.EntityFrameworkCore.Sample",
"program": "${workspaceFolder}/samples/Repository/Skidbkadnir.Repository.EntityFrameworkCore.Sample/bin/Debug/netcoreapp3.1/Skidbkadnir.Repository.EntityFrameworkCore.Sample.dll",
"args": [],
"cwd": "${workspaceFolder}/samples/Repository/Skidbkadnir.Repository.EntityFrameworkCore.Sample",
"stopAtEntry": false,
"console": "internalConsole"
},
{
"name": "Skidbladnir.Modules.Sample",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build-Skidbladnir.Modules.Sample",
"program": "${workspaceFolder}/samples/Modules/Skidbladnir.Modules.Sample/bin/Debug/netcoreapp3.1/Skidbladnir.Modules.Sample.dll",
"args": [],
"cwd": "${workspaceFolder}/samples/Modules/Skidbladnir.Modules.Sample",
"stopAtEntry": false,
"console": "internalConsole"
},
{
"name": "Skidbladnir.Messaging.Redis.Sample",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build-Skidbladnir.Messaging.Redis.Sample",
"program": "${workspaceFolder}/samples/Messaging/Skidbladnir.Messaging.Redis.Sample/bin/Debug/netcoreapp3.1/Skidbladnir.Messaging.Redis.Sample.dll",
"args": [],
"cwd": "${workspaceFolder}/samples/Messaging/Skidbladnir.Messaging.Redis.Sample",
"stopAtEntry": false,
"console": "internalConsole"
},
{
"name": "Skidbladnir.Client.Freenom.Dns.Sample",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build-Skidbladnir.Client.Freenom.Dns.Sample",
"program": "${workspaceFolder}/samples/Client/Skidbladnir.Client.Freenom.Dns.Sample/bin/Debug/netcoreapp3.1/Skidbladnir.Client.Freenom.Dns.Sample.dll",
"args": [],
"cwd": "${workspaceFolder}/samples/Client/Skidbladnir.Client.Freenom.Dns.Sample",
"stopAtEntry": false,
"console": "internalConsole"
}
]
{
"version": "0.2.0",
"configurations": [
{
"name": "Skidbladnir.Storage.GridFsStorage.Sample",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build-Skidbladnir.Storage.GridFsStorage.Sample",
"program": "${workspaceFolder}/samples/Storage/Skidbladnir.Storage.GridFsStorage.Sample/bin/Debug/netcoreapp3.1/Skidbladnir.Storage.GridFsStorage.Sample.dll",
"args": [],
"cwd": "${workspaceFolder}/samples/Storage/Skidbladnir.Storage.GridFsStorage.Sample",
"stopAtEntry": false,
"console": "internalConsole"
},
{
"name": "Skidbladnir.Storage.LocalFileStorage.Sample",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build-Skidbladnir.Storage.LocalFileStorage.Sample",
"program": "${workspaceFolder}/samples/Storage/Skidbladnir.Storage.LocalFileStorage.Sample/bin/Debug/netcoreapp3.1/Skidbladnir.Storage.LocalFileStorage.Sample.dll",
"args": [],
"cwd": "${workspaceFolder}/samples/Storage/Skidbladnir.Storage.LocalFileStorage.Sample",
"stopAtEntry": false,
"console": "internalConsole"
},
{
"name": "Skidbladnir.Storage.WebDav.Sample",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build-Skidbladnir.Storage.WebDav.Sample",
"program": "${workspaceFolder}/samples/Storage/Skidbladnir.Storage.WebDav.Sample/bin/Debug/netcoreapp3.1/Skidbladnir.Storage.WebDav.Sample.dll",
"args": [],
"cwd": "${workspaceFolder}/samples/Storage/Skidbladnir.Storage.WebDav.Sample",
"stopAtEntry": false,
"console": "internalConsole"
},
{
"name": "Skidbkadnir.Repository.EntityFrameworkCore.Sample",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build-Skidbkadnir.Repository.EntityFrameworkCore.Sample",
"program": "${workspaceFolder}/samples/Repository/Skidbkadnir.Repository.EntityFrameworkCore.Sample/bin/Debug/netcoreapp3.1/Skidbkadnir.Repository.EntityFrameworkCore.Sample.dll",
"args": [],
"cwd": "${workspaceFolder}/samples/Repository/Skidbkadnir.Repository.EntityFrameworkCore.Sample",
"stopAtEntry": false,
"console": "internalConsole"
},
{
"name": "Skidbladnir.Modules.Sample",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build-Skidbladnir.Modules.Sample",
"program": "${workspaceFolder}/samples/Modules/Skidbladnir.Modules.Sample/bin/Debug/netcoreapp3.1/Skidbladnir.Modules.Sample.dll",
"args": [],
"cwd": "${workspaceFolder}/samples/Modules/Skidbladnir.Modules.Sample",
"stopAtEntry": false,
"console": "internalConsole"
},
{
"name": "Skidbladnir.Messaging.Redis.Sample",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build-Skidbladnir.Messaging.Redis.Sample",
"program": "${workspaceFolder}/samples/Messaging/Skidbladnir.Messaging.Redis.Sample/bin/Debug/netcoreapp3.1/Skidbladnir.Messaging.Redis.Sample.dll",
"args": [],
"cwd": "${workspaceFolder}/samples/Messaging/Skidbladnir.Messaging.Redis.Sample",
"stopAtEntry": false,
"console": "internalConsole"
},
{
"name": "Skidbladnir.Client.Freenom.Dns.Sample",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build-Skidbladnir.Client.Freenom.Dns.Sample",
"program": "${workspaceFolder}/samples/Client/Skidbladnir.Client.Freenom.Dns.Sample/bin/Debug/netcoreapp3.1/Skidbladnir.Client.Freenom.Dns.Sample.dll",
"args": [],
"cwd": "${workspaceFolder}/samples/Client/Skidbladnir.Client.Freenom.Dns.Sample",
"stopAtEntry": false,
"console": "internalConsole"
},
{
"name": "Skidbladnir.Storage.S3.Sample",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build-Skidbladnir.Storage.S3.Sample",
"program": "${workspaceFolder}/samples/Storage/Skidbladnir.Storage.S3.Sample/bin/Debug/netcoreapp3.1/Skidbladnir.Storage.S3.Sample.dll",
"args": [],
"env": {
"DOTNET_ENVIRONMENT": "Development"
},
"cwd": "${workspaceFolder}/samples/Storage/Skidbladnir.Storage.S3.Sample",
"stopAtEntry": false,
"console": "internalConsole"
}
]
}
12 changes: 12 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "build-Skidbladnir.Storage.S3.Sample",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/samples/Storage/Skidbladnir.Storage.S3.Sample/Skidbladnir.Storage.S3.Sample.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
}
]
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ The repository contains the following directions and libraries:
- [Storage.GridFs](src/Storage/Skidbladnir.Storage.GridFS/README.md) - GridFs storage implementation
- [Storage.LocalFs](src/Storage/Skidbladnir.Storage.LocalFileStorage/README.md) - Local file system storage implementation
- [Storage.WebDav](src/Storage/Skidbladnir.Storage.WebDav/README.md) - Implementing a file storage abstraction based on WebDav protocol
- [Storage.S3](src/Storage/Skidbladnir.Storage.S3/README.md) - Implementing a file storage abstraction based on S3 protocol
- [Messaging](src/Messaging/README.md) - Pub/Sub and Send/Receive abstraction
- [Messaging.Redis](src/Messaging/Skidbladnir.Messaging.Redis/README.md) - Redis implementation Pub/Sub and Send/Receive message bus
- [Utilities](src/Utility/Skidbladnir.Utility.Common/README.md) - A set of utilities to simplify development
14 changes: 14 additions & 0 deletions Skidbladnir.sln
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Skidbladnir.Storage.WebDav"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Skidbladnir.Storage.WebDav.Sample", "samples\Storage\Skidbladnir.Storage.WebDav.Sample\Skidbladnir.Storage.WebDav.Sample.csproj", "{E1110F0B-FD10-4D2E-A1D9-92108F41B371}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Skidbladnir.Storage.S3", "src\Storage\Skidbladnir.Storage.S3\Skidbladnir.Storage.S3.csproj", "{801AF4DE-FB84-43D7-8907-F48AC9DCE05B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Skidbladnir.Storage.S3.Sample", "samples\Storage\Skidbladnir.Storage.S3.Sample\Skidbladnir.Storage.S3.Sample.csproj", "{55BB43CF-2D6E-4E85-B6E3-23B6BEF04A4D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -175,6 +179,14 @@ Global
{E1110F0B-FD10-4D2E-A1D9-92108F41B371}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E1110F0B-FD10-4D2E-A1D9-92108F41B371}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E1110F0B-FD10-4D2E-A1D9-92108F41B371}.Release|Any CPU.Build.0 = Release|Any CPU
{801AF4DE-FB84-43D7-8907-F48AC9DCE05B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{801AF4DE-FB84-43D7-8907-F48AC9DCE05B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{801AF4DE-FB84-43D7-8907-F48AC9DCE05B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{801AF4DE-FB84-43D7-8907-F48AC9DCE05B}.Release|Any CPU.Build.0 = Release|Any CPU
{55BB43CF-2D6E-4E85-B6E3-23B6BEF04A4D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{55BB43CF-2D6E-4E85-B6E3-23B6BEF04A4D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{55BB43CF-2D6E-4E85-B6E3-23B6BEF04A4D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{55BB43CF-2D6E-4E85-B6E3-23B6BEF04A4D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -216,6 +228,8 @@ Global
{1CEE19DA-64D6-47D2-AF3C-3C0AAF8B5C58} = {079188FC-DF1D-4C55-854F-A4BAF65FEDAC}
{ECB26992-FBE0-47D6-8C0E-4A9CC953D506} = {3BD793D1-0CD1-43BC-8F90-38C352ABE4FA}
{E1110F0B-FD10-4D2E-A1D9-92108F41B371} = {5B76C393-EF50-410E-A665-079A7A176DC7}
{801AF4DE-FB84-43D7-8907-F48AC9DCE05B} = {3BD793D1-0CD1-43BC-8F90-38C352ABE4FA}
{55BB43CF-2D6E-4E85-B6E3-23B6BEF04A4D} = {5B76C393-EF50-410E-A665-079A7A176DC7}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F5502508-B14D-4DDE-9CCF-2A62CF7C4F71}
Expand Down
24 changes: 24 additions & 0 deletions samples/Storage/Skidbladnir.Storage.S3.Sample/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Skidbladnir.Modules;

namespace Skidbladnir.Storage.S3.Sample
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSkidbladnirModules<StartupModule>(configuration =>
{
var storageConfiguration = new S3StorageConfiguration();
configuration.AppConfiguration.GetSection("Storage").Bind(storageConfiguration);
configuration.Add(storageConfiguration);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk.Worker">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<UserSecretsId>331ba4d8-efbc-4df6-ac78-d12ab711db05</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.10" />

<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="3.1.10" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\Modules\Skidbladnir.Modules\Skidbladnir.Modules.csproj" />
<ProjectReference Include="..\..\..\src\Storage\Skidbladnir.Storage.Abstractions\Skidbladnir.Storage.Abstractions.csproj" />
<ProjectReference Include="..\..\..\src\Storage\Skidbladnir.Storage.S3\Skidbladnir.Storage.S3.csproj" />
</ItemGroup>
</Project>
107 changes: 107 additions & 0 deletions samples/Storage/Skidbladnir.Storage.S3.Sample/StartupModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Skidbladnir.Modules;
using Skidbladnir.Storage.Abstractions;
using FileInfo = Skidbladnir.Storage.Abstractions.FileInfo;
namespace Skidbladnir.Storage.S3.Sample
{
public class StartupModule : RunnableModule
{
private ILogger<StartupModule> _logger;
private IStorage<S3StorageInfo> _storage;

public override void Configure(IServiceCollection services)
{
var storageConfiguration = Configuration.Get<S3StorageConfiguration>();
services.AddS3Storage(storageConfiguration);
}

public override async Task StartAsync(IServiceProvider provider, CancellationToken cancellationToken)
{
_logger = provider.GetService<ILogger<StartupModule>>();
_storage = provider.GetService<IStorage<S3StorageInfo>>();
try
{
var testString = $"This is a test text for write to file {DateTime.UtcNow}";
var testBinary = Encoding.UTF8.GetBytes(testString);

var currentDir = await _storage.GetFilesAsync("");
_logger.LogInformation("Files in root dir");
foreach (var fileInfo in currentDir)
{
_logger.LogInformation("Filename: {FileName}\t\t Length: {Length}\t\t Date: {Date}",
fileInfo.FileName,
fileInfo.Size, fileInfo.CreatedDate);
await DownloadFileAndPrint(fileInfo);
}

_logger.LogInformation("upload file:");
var testFileUploadStream = new MemoryStream(testBinary);
var uploadFileinfo = await _storage.UploadFileAsync(testFileUploadStream, "testFile.txt");
_logger.LogInformation("Filename: {FileName}\t\t Length: {Length}\t\t Date: {Date}",
uploadFileinfo.FileName,
uploadFileinfo.Size, uploadFileinfo.CreatedDate);

_logger.LogInformation("copy file");
await _storage.CopyAsync(uploadFileinfo.FilePath, $"{uploadFileinfo.FilePath}.new");
await _storage.CopyAsync(uploadFileinfo.FilePath,
$"newFolder{Path.DirectorySeparatorChar}{uploadFileinfo.FilePath}.new");

_logger.LogInformation("move file");
await _storage.MoveAsync($"{uploadFileinfo.FilePath}.new", $"{uploadFileinfo.FilePath}.backup");
await _storage.CopyAsync($"{uploadFileinfo.FilePath}.backup", $"very/long/sub/dir/path/{uploadFileinfo.FilePath}");

_logger.LogInformation("remove base file");
await _storage.DeleteAsync(uploadFileinfo.FilePath);

_logger.LogInformation("Files in root dir");
currentDir = await _storage.GetFilesAsync("");
foreach (var fileInfo in currentDir)
{
_logger.LogInformation("Filename: {FileName}\t\t Length: {Length}\t\t Date: {Date}",
fileInfo.FileName,
fileInfo.Size, fileInfo.CreatedDate);
await DownloadFileAndPrint(fileInfo);
}

_logger.LogInformation("Files in newFolder dir");
currentDir = await _storage.GetFilesAsync("newFolder");
foreach (var fileInfo in currentDir)
{
_logger.LogInformation("Filename: {FileName}\t\t Length: {Length}\t\t Date: {Date}",
fileInfo.FileName,
fileInfo.Size, fileInfo.CreatedDate);
await DownloadFileAndPrint(fileInfo);
}

_logger.LogInformation("Files in very/long/sub/dir/path/ dir");
currentDir = await _storage.GetFilesAsync("very/long/sub/dir/path/");
foreach (var fileInfo in currentDir)
{
_logger.LogInformation("Filename: {FileName}\t\t Length: {Length}\t\t Date: {Date}",
fileInfo.FileName,
fileInfo.Size, fileInfo.CreatedDate);
await DownloadFileAndPrint(fileInfo);
}
}
catch (Exception e)
{
_logger.LogError(e, "Error in storage sample");
}
}

private async Task DownloadFileAndPrint(FileInfo info)
{
var result = await _storage.DownloadFileAsync(info.FilePath);
await using var fileStream = result.Content;
using var streamReader = new StreamReader(fileStream);
var fileContent = await streamReader.ReadToEndAsync();
_logger.LogInformation("File Content: {Content}", fileContent);
}
}
}
Loading

0 comments on commit e46f085

Please sign in to comment.