From c2bd8fac3e2ace56726c1c85960df9e161950168 Mon Sep 17 00:00:00 2001 From: Mikayla Dobson Date: Fri, 17 Nov 2023 15:15:55 -0600 Subject: [PATCH] lay out boilerplate for new s3 integration --- Unbinder/Services/S3Service.cs | 44 ++++++++++++++++++++++++++++++++++ Unbinder/Unbinder.csproj | 1 + 2 files changed, 45 insertions(+) create mode 100644 Unbinder/Services/S3Service.cs diff --git a/Unbinder/Services/S3Service.cs b/Unbinder/Services/S3Service.cs new file mode 100644 index 0000000..ac79a55 --- /dev/null +++ b/Unbinder/Services/S3Service.cs @@ -0,0 +1,44 @@ +using Amazon; +using Amazon.S3; +using Amazon.S3.Transfer; +using Amazon.Runtime; + +namespace Unbinder.Services +{ + public class S3Service + { + public static AWSCredentials Credentials => + new BasicAWSCredentials( + Environment.GetEnvironmentVariable("AWS_ACCESS_KEY"), + Environment.GetEnvironmentVariable("AWS_SECRET_KEY")); + + public static AmazonS3Client Client => new(Credentials, RegionEndpoint.USEast1); + + public async Task UploadFileAsync(IFormFile file) + { + using IAmazonS3 client = Client; + + if (file == null || file.Length == 0) + { + return null; + } + + var bucketName = "unbinder-recipe-images"; + var keyName = file.FileName; + + using var fileTransferUtility = new TransferUtility(client); + await fileTransferUtility.UploadAsync(file.OpenReadStream(), bucketName, keyName); + + return keyName; + } + + public async Task GetFile(string path, string outFile) + { + using IAmazonS3 client = Client; + + var bucketName = "unbinder-recipe-images"; + using var fileTransferUtility = new TransferUtility(client); + await fileTransferUtility.DownloadAsync(outFile, bucketName, path); + } + } +} diff --git a/Unbinder/Unbinder.csproj b/Unbinder/Unbinder.csproj index cc3c067..6eee0b0 100644 --- a/Unbinder/Unbinder.csproj +++ b/Unbinder/Unbinder.csproj @@ -9,6 +9,7 @@ +