From 38f4d1da3d2d837189dfc0e3ae25ee911022713c Mon Sep 17 00:00:00 2001 From: Lilian ARAGO Date: Fri, 10 Feb 2023 15:49:01 +0100 Subject: [PATCH 01/16] Uploading files to S3 --- TCC.Lib/Options/CompressOption.cs | 9 +++-- TCC.Lib/Storage/RemoteStorageFactory.cs | 23 +++++++++++- TCC.Lib/Storage/S3RemoteStorage.cs | 48 +++++++++++++++++++++++++ TCC.Lib/TCC.Lib.csproj | 1 + TCC.Lib/UploadMode.cs | 5 +-- TCC/Parser/CompressCommand.cs | 7 +++- 6 files changed, 87 insertions(+), 6 deletions(-) create mode 100644 TCC.Lib/Storage/S3RemoteStorage.cs diff --git a/TCC.Lib/Options/CompressOption.cs b/TCC.Lib/Options/CompressOption.cs index f64aef4..391a1f1 100644 --- a/TCC.Lib/Options/CompressOption.cs +++ b/TCC.Lib/Options/CompressOption.cs @@ -21,7 +21,12 @@ public class CompressOption : TccOption 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; } + 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 UploadMode? UploadMode { get; set; } } } \ No newline at end of file diff --git a/TCC.Lib/Storage/RemoteStorageFactory.cs b/TCC.Lib/Storage/RemoteStorageFactory.cs index 8d97434..f72763e 100644 --- a/TCC.Lib/Storage/RemoteStorageFactory.cs +++ b/TCC.Lib/Storage/RemoteStorageFactory.cs @@ -1,4 +1,6 @@ -using Azure.Storage.Blobs; +using Amazon.Runtime; +using Amazon.S3; +using Azure.Storage.Blobs; using Google.Apis.Auth.OAuth2; using Google.Cloud.Storage.V1; using Microsoft.Extensions.Logging; @@ -42,6 +44,25 @@ public static async Task GetRemoteStorageAsync(this CompressOpti StorageClient storage = await GoogleAuthHelper.GetGoogleStorageClientAsync(option.GoogleStorageCredential, token); return new GoogleRemoteStorage(storage, option.GoogleStorageBucketName); } + case UploadMode.S3: + if (string.IsNullOrEmpty(option.S3AccessKeyId) + || string.IsNullOrEmpty(option.S3Host) + || string.IsNullOrEmpty(option.S3Region) + || string.IsNullOrEmpty(option.S3BucketName) + || string.IsNullOrEmpty(option.S3SecretAcessKey)) + { + logger.LogCritical("Configuration error for S3 upload"); + return new NoneRemoteStorage(); + } + + var credentials = new BasicAWSCredentials(option.S3AccessKeyId, option.S3SecretAcessKey); + var s3Config = new AmazonS3Config() + { + AuthenticationRegion = option.S3Region, + ServiceURL = option.S3Host, + }; + + return new S3RemoteStorage(new AmazonS3Client(credentials, s3Config), option.S3BucketName); case UploadMode.None: case null: return new NoneRemoteStorage(); diff --git a/TCC.Lib/Storage/S3RemoteStorage.cs b/TCC.Lib/Storage/S3RemoteStorage.cs new file mode 100644 index 0000000..2e0533f --- /dev/null +++ b/TCC.Lib/Storage/S3RemoteStorage.cs @@ -0,0 +1,48 @@ +using Amazon; +using Amazon.Runtime; +using Amazon.S3; +using System; +using System.IO; +using System.Threading; +using System.Threading.Tasks; + +namespace TCC.Lib.Storage +{ + public class S3RemoteStorage : IRemoteStorage + { + internal string BucketName { get; } + private readonly AmazonS3Client _s3Client; + + public S3RemoteStorage(AmazonS3Client s3Client, string bucketName) + { + BucketName = bucketName; + _s3Client = s3Client; + } + + public async Task UploadAsync(string targetPath, Stream data, CancellationToken token) + { + try + { + await _s3Client.PutObjectAsync(new Amazon.S3.Model.PutObjectRequest() { + BucketName = BucketName, + Key = targetPath, + InputStream = data, + }, token); + } + catch (Exception e) + { + return new UploadResponse + { + IsSuccess = false, + RemoteFilePath = targetPath, + ErrorMessage = e.Message + }; + } + return new UploadResponse + { + IsSuccess = true, + RemoteFilePath = targetPath + }; + } + } +} \ No newline at end of file diff --git a/TCC.Lib/TCC.Lib.csproj b/TCC.Lib/TCC.Lib.csproj index 8534aae..25043fa 100644 --- a/TCC.Lib/TCC.Lib.csproj +++ b/TCC.Lib/TCC.Lib.csproj @@ -6,6 +6,7 @@ + diff --git a/TCC.Lib/UploadMode.cs b/TCC.Lib/UploadMode.cs index 6ff5896..ebfae59 100644 --- a/TCC.Lib/UploadMode.cs +++ b/TCC.Lib/UploadMode.cs @@ -1,9 +1,10 @@ -namespace TCC.Lib +namespace TCC.Lib { public enum UploadMode { None, AzureSdk, - GoogleCloudStorage + GoogleCloudStorage, + S3 } } \ No newline at end of file diff --git a/TCC/Parser/CompressCommand.cs b/TCC/Parser/CompressCommand.cs index 8814060..9cbcc87 100644 --- a/TCC/Parser/CompressCommand.cs +++ b/TCC/Parser/CompressCommand.cs @@ -64,7 +64,12 @@ protected override IEnumerable