This repository was archived by the owner on Sep 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
S3 storage implementation #47
Open
NahisWayard
wants to merge
16
commits into
master
Choose a base branch
from
s3-storage
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
38f4d1d
Uploading files to S3
58b5ade
Multiple uploadModes
f6d2eee
Fix progression display
feee3d2
Multipart upload
0fbcf58
Fixed sonarcloud code smells
4e5a84d
Fixed some code smells
ecc3b68
Fix code smells
848d7bd
Fixed code smell
eb88fb6
string.ParseSize return -1 in case of error
2a3bfb5
Removed ParseSizeInteral
14840cc
Explicit error messages when nstantiating uploaders
d5528b5
Gitignore .idea
e38f97b
Using AsyncEnumerable for GetRemoteStoragesAsync
e0345b7
Clearer code
42b3770
Implemtented ReadOnlyChunkedStream
c85ed27
Fixed code smells
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,13 @@ | ||
| .vs/* | ||
| */obj/* | ||
| */bin/* | ||
| packages | ||
| *.user | ||
| /coverage-opencover.xml | ||
| launchSettings.json | ||
| */logs/* | ||
| *.db | ||
| /TCC.Tests/'coverage.xml' | ||
| /coverage.xml | ||
| /published | ||
| .vs/* | ||
| */obj/* | ||
| */bin/* | ||
| packages | ||
| *.user | ||
| /coverage-opencover.xml | ||
| launchSettings.json | ||
| */logs/* | ||
| *.db | ||
| /TCC.Tests/'coverage.xml' | ||
| /coverage.xml | ||
| /published | ||
| .idea |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,102 +1,120 @@ | ||
| using System; | ||
| using System.Globalization; | ||
| using System.IO; | ||
|
|
||
| namespace TCC.Lib.Helpers | ||
| { | ||
| public static class StringExtensions | ||
| { | ||
| public static string Escape(this string str) | ||
| { | ||
| return '"' + str.Trim('"') + '"'; | ||
| } | ||
|
|
||
| public static string HumanizedBandwidth(this double bandwidth, int decimals = 2) | ||
| { | ||
| var ordinals = new[] { "", "K", "M", "G", "T", "P", "E" }; | ||
| var rate = (decimal)bandwidth; | ||
| var ordinal = 0; | ||
| while (rate > 1024) | ||
| { | ||
| rate /= 1024; | ||
| ordinal++; | ||
| } | ||
| return String.Format("{0:n" + decimals + "} {1}b/s", Math.Round(rate, decimals, MidpointRounding.AwayFromZero), ordinals[ordinal]); | ||
| } | ||
|
|
||
| public static String HumanizeSize(this long size) | ||
| { | ||
| string[] suf = { "B", "Ko", "Mo", "Go", "To", "Po", "Eo" }; | ||
| if (size == 0) | ||
| return "0" + suf[0]; | ||
| long bytes = Math.Abs(size); | ||
| int place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024))); | ||
| double num = Math.Round(bytes / Math.Pow(1024, place), 1); | ||
| return (Math.Sign(size) * num).ToString(CultureInfo.InvariantCulture) + " " + suf[place]; | ||
| } | ||
|
|
||
| public static string HumanizedTimeSpan(this TimeSpan t, int parts = 2) | ||
| { | ||
| string result = string.Empty; | ||
| if (t.TotalDays >= 1 && parts > 0) | ||
| { | ||
| result += $"{t.Days}d "; | ||
| parts--; | ||
| } | ||
| if (t.TotalHours >= 1 && parts > 0) | ||
| { | ||
| result += $"{t.Hours}h "; | ||
| parts--; | ||
| } | ||
| if (t.TotalMinutes >= 1 && parts > 0) | ||
| { | ||
| result += $"{t.Minutes}m "; | ||
| parts--; | ||
| } | ||
| if (t.Seconds >= 1 && parts > 0) | ||
| { | ||
| result += $"{t.Seconds}s "; | ||
| parts--; | ||
| } | ||
| if (t.Milliseconds >= 1 && parts > 0) | ||
| { | ||
| result += $"{t.Milliseconds}ms"; | ||
| } | ||
| return result.TrimEnd(); | ||
| } | ||
|
|
||
| public static string Pad(this string source, int length) | ||
| { | ||
| if (source == null) | ||
| { | ||
| return new string(' ', length); | ||
| } | ||
| return source.Length > length ? source.Substring(0, length) : source.PadLeft(length, ' '); | ||
| } | ||
|
|
||
| public static (string Name, DateTime? Date) ExtractArchiveNameAndDate(this string filePath) | ||
| { | ||
| if (filePath == null) | ||
| throw new ArgumentNullException(nameof(filePath)); | ||
|
|
||
| string segment = Path.GetFileNameWithoutExtension(filePath); | ||
| if (segment.EndsWith(".diff") || segment.EndsWith(".full")) | ||
| { | ||
| segment = segment.Substring(0, segment.Length - 5); | ||
| } | ||
|
|
||
| var lastSegment = segment.LastIndexOf('_'); | ||
| if (lastSegment > 0) | ||
| { | ||
| string name = segment.Substring(0, lastSegment); | ||
| string date = segment.Substring(lastSegment + 1); | ||
| if (date.TryParseArchiveDateTime(out var dt)) | ||
| { | ||
| return (name, dt); | ||
| } | ||
| return (name, null); | ||
| } | ||
| return (segment, null); | ||
| } | ||
| } | ||
| using System; | ||
| using System.Globalization; | ||
| using System.IO; | ||
| using System.Linq; | ||
|
|
||
| namespace TCC.Lib.Helpers | ||
| { | ||
| public static class StringExtensions | ||
| { | ||
| public static string Escape(this string str) | ||
| { | ||
| return '"' + str.Trim('"') + '"'; | ||
| } | ||
|
|
||
| public static string HumanizedBandwidth(this double bandwidth, int decimals = 2) | ||
| { | ||
| var ordinals = new[] { "", "K", "M", "G", "T", "P", "E" }; | ||
| var rate = (decimal)bandwidth; | ||
| var ordinal = 0; | ||
| while (rate > 1024) | ||
| { | ||
| rate /= 1024; | ||
| ordinal++; | ||
| } | ||
| return String.Format("{0:n" + decimals + "} {1}b/s", Math.Round(rate, decimals, MidpointRounding.AwayFromZero), ordinals[ordinal]); | ||
| } | ||
|
|
||
| public static String HumanizeSize(this long size) | ||
| { | ||
| string[] suf = { "B", "Ko", "Mo", "Go", "To", "Po", "Eo" }; | ||
| if (size == 0) | ||
| return "0" + suf[0]; | ||
| long bytes = Math.Abs(size); | ||
| int place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024))); | ||
| double num = Math.Round(bytes / Math.Pow(1024, place), 1); | ||
| return (Math.Sign(size) * num).ToString(CultureInfo.InvariantCulture) + " " + suf[place]; | ||
| } | ||
|
|
||
| public static long ParseSize(this string humanizedSize) | ||
| { | ||
| if (string.IsNullOrWhiteSpace(humanizedSize)) | ||
| return -1; | ||
| string[] suf = { "b", "ko", "mo", "go", "to", "po", "eo" }; | ||
| var size = humanizedSize.Trim().ToLower(CultureInfo.InvariantCulture); | ||
| var number = string.Join("", size.Where(char.IsDigit)); | ||
| var unit = size.Substring(size.Length - 2); | ||
| var pow = Array.IndexOf(suf, unit); | ||
|
|
||
| return pow switch | ||
| { | ||
| -1 => long.Parse(number, CultureInfo.InvariantCulture), | ||
| _ => long.Parse(number, CultureInfo.InvariantCulture) * (long)Math.Pow(1024L, pow) | ||
| }; | ||
| } | ||
|
|
||
| public static string HumanizedTimeSpan(this TimeSpan t, int parts = 2) | ||
| { | ||
| string result = string.Empty; | ||
| if (t.TotalDays >= 1 && parts > 0) | ||
| { | ||
| result += $"{t.Days}d "; | ||
| parts--; | ||
| } | ||
| if (t.TotalHours >= 1 && parts > 0) | ||
| { | ||
| result += $"{t.Hours}h "; | ||
| parts--; | ||
| } | ||
| if (t.TotalMinutes >= 1 && parts > 0) | ||
| { | ||
| result += $"{t.Minutes}m "; | ||
| parts--; | ||
| } | ||
| if (t.Seconds >= 1 && parts > 0) | ||
| { | ||
| result += $"{t.Seconds}s "; | ||
| parts--; | ||
| } | ||
| if (t.Milliseconds >= 1 && parts > 0) | ||
| { | ||
| result += $"{t.Milliseconds}ms"; | ||
| } | ||
| return result.TrimEnd(); | ||
| } | ||
|
|
||
| public static string Pad(this string source, int length) | ||
| { | ||
| if (source == null) | ||
| { | ||
| return new string(' ', length); | ||
| } | ||
| return source.Length > length ? source.Substring(0, length) : source.PadLeft(length, ' '); | ||
| } | ||
|
|
||
| public static (string Name, DateTime? Date) ExtractArchiveNameAndDate(this string filePath) | ||
| { | ||
| if (filePath == null) | ||
| throw new ArgumentNullException(nameof(filePath)); | ||
|
|
||
| string segment = Path.GetFileNameWithoutExtension(filePath); | ||
| if (segment.EndsWith(".diff") || segment.EndsWith(".full")) | ||
| { | ||
| segment = segment.Substring(0, segment.Length - 5); | ||
| } | ||
|
|
||
| var lastSegment = segment.LastIndexOf('_'); | ||
| if (lastSegment > 0) | ||
| { | ||
| string name = segment.Substring(0, lastSegment); | ||
| string date = segment.Substring(lastSegment + 1); | ||
| if (date.TryParseArchiveDateTime(out var dt)) | ||
| { | ||
| return (name, dt); | ||
| } | ||
| return (name, null); | ||
| } | ||
| return (segment, null); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,36 @@ | ||
| using System.Collections.Generic; | ||
| using TCC.Lib.Blocks; | ||
| using TCC.Lib.Database; | ||
|
|
||
| namespace TCC.Lib.Options | ||
| { | ||
| public class CompressOption : TccOption | ||
| { | ||
| public BlockMode BlockMode { get; set; } | ||
| public CompressionAlgo Algo { get; set; } | ||
| public int CompressionRatio { get; set; } | ||
| public BackupMode? BackupMode { get; set; } | ||
| public int? RetryPeriodInSeconds { get; set; } | ||
| public IEnumerable<string> Filter { get; set; } | ||
| public IEnumerable<string> Exclude { get; set; } | ||
| public bool FolderPerDay { get; set; } | ||
| public int? BoostRatio { get; set; } | ||
| public int? CleanupTime { get; set; } | ||
| public string AzBlobUrl { get; set; } | ||
| public string AzBlobContainer { get; set; } | ||
| public string AzBlobSaS { get; set; } | ||
| public int? AzThread { get; set; } | ||
| public string GoogleStorageBucketName { get; set; } | ||
| public string GoogleStorageCredential { get; set; } | ||
| public UploadMode? UploadMode { get; set; } | ||
| } | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using TCC.Lib.Blocks; | ||
| using TCC.Lib.Database; | ||
|
|
||
| namespace TCC.Lib.Options | ||
| { | ||
| public class CompressOption : TccOption | ||
| { | ||
| public BlockMode BlockMode { get; set; } | ||
| public CompressionAlgo Algo { get; set; } | ||
| public int CompressionRatio { get; set; } | ||
| public BackupMode? BackupMode { get; set; } | ||
| public int? RetryPeriodInSeconds { get; set; } | ||
| public IEnumerable<string> Filter { get; set; } | ||
| public IEnumerable<string> Exclude { get; set; } | ||
| public bool FolderPerDay { get; set; } | ||
| public int? BoostRatio { get; set; } | ||
| public int? CleanupTime { get; set; } | ||
| public string AzBlobUrl { get; set; } | ||
| public string AzBlobContainer { get; set; } | ||
| public string AzBlobSaS { get; set; } | ||
| public int? AzThread { get; set; } | ||
| public string GoogleStorageBucketName { get; set; } | ||
| public string GoogleStorageCredential { get; set; } | ||
| public string S3AccessKeyId { get; set; } | ||
| public string S3SecretAcessKey { get; set; } | ||
| public string S3Host { get; set; } | ||
| public string S3BucketName { get; set; } | ||
| public string S3Region { get; set; } | ||
| public string S3MultipartThreshold { get; set; } | ||
| public string S3MultipartSize { get; set; } | ||
| public IEnumerable<UploadMode> UploadModes { get; set; } = Enumerable.Empty<UploadMode>(); | ||
NahisWayard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public UploadMode? UploadMode { get; set; } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| using System; | ||
| using System.IO; | ||
|
|
||
| namespace TCC.Lib | ||
| { | ||
| public class ReadOnlyChunkedStream : Stream | ||
| { | ||
| private readonly Stream _inputStream; | ||
| private readonly int _chunkSize; | ||
|
|
||
| public ReadOnlyChunkedStream(Stream inputStream, int chunkSize) | ||
| { | ||
| _inputStream = inputStream; | ||
| _chunkSize = chunkSize; | ||
| } | ||
|
|
||
| public override void Flush() => _inputStream.Flush(); | ||
|
|
||
| public override int Read(byte[] buffer, int offset, int count) | ||
| { | ||
| var maxRead = (int)Math.Min(count, _chunkSize - Position); | ||
| var nbRead = _inputStream.Read(buffer, offset, maxRead); | ||
| Position += nbRead; | ||
| return nbRead; | ||
| } | ||
|
|
||
| public override long Seek(long offset, SeekOrigin origin) | ||
| { | ||
| return _inputStream.Seek(offset, origin); | ||
| } | ||
|
|
||
| public override void SetLength(long value) | ||
| { | ||
| throw new NotSupportedException(); | ||
| } | ||
|
|
||
| public override void Write(byte[] buffer, int offset, int count) | ||
| { | ||
| throw new NotSupportedException(); | ||
| } | ||
|
|
||
| public override bool CanRead => _inputStream.CanRead; | ||
| public override bool CanSeek => _inputStream.CanSeek; | ||
| public override bool CanWrite => false; | ||
| public override long Length => Math.Min(_chunkSize, _inputStream.Length - _inputStream.Position); | ||
| public override long Position { get; set; } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.