From 47a82e18a7b5765a9b6a4f0626c1cb67d6b64939 Mon Sep 17 00:00:00 2001 From: Mikayla Dobson Date: Mon, 4 Dec 2023 09:51:11 -0600 Subject: [PATCH] fixes for s3 integration --- Unbinder/Controllers/HomeController.cs | 47 ++++++++++++------------ Unbinder/Program.cs | 9 ++--- Unbinder/Services/S3Service.cs | 49 ++++++++++++-------------- Unbinder/Views/Home/Index.cshtml | 15 +++++++- Unbinder/Views/Home/Index.cshtml.cs | 12 +++---- Unbinder/Views/_ViewImports.cshtml | 1 + 6 files changed, 71 insertions(+), 62 deletions(-) diff --git a/Unbinder/Controllers/HomeController.cs b/Unbinder/Controllers/HomeController.cs index f631d1b..5964b20 100644 --- a/Unbinder/Controllers/HomeController.cs +++ b/Unbinder/Controllers/HomeController.cs @@ -1,6 +1,5 @@ using Amazon.S3; using Microsoft.AspNetCore.Mvc; -using Microsoft.VisualBasic; using Unbinder.Services; namespace Unbinder.Controllers @@ -11,36 +10,40 @@ namespace Unbinder.Controllers { try { - var objects = await S3Service.ListObjects(); + S3Service s3Service = new(); + var response = await s3Service.ListObjects(); - if (objects != null) - { - string keys = ""; - - foreach (var entry in objects.S3Objects) - { - keys += entry.Key + ", "; - } - - if (keys != "") logger.Log(LogLevel.Information, $"Found keys: {keys ?? "(none)"}"); - } - else + if (response == null) { logger.Log(LogLevel.Debug, "Did not find results in S3"); + return View(); } + + string keys = ""; + foreach (var entry in response.S3Objects) + { + keys += entry.Key + ", "; + } + + if (keys != "") logger.Log(LogLevel.Information, $"Found keys: {keys ?? "(none)"}"); + return View(response.S3Objects); } catch (Exception ex) { - if (ex is AmazonS3Exception s3Exception) - { - logger.Log(LogLevel.Warning, s3Exception.ErrorCode); - logger.Log(LogLevel.Warning, s3Exception.Message); - } - else - logger.Log(LogLevel.Error, ex.Message); + HandleError(ex, logger); + return View(); } + } - return View(); + private static void HandleError(Exception ex, ILogger logger) + { + if (ex is AmazonS3Exception s3Exception) + { + logger.Log(LogLevel.Warning, s3Exception.ErrorCode); + logger.Log(LogLevel.Warning, s3Exception.Message); + } + else + logger.Log(LogLevel.Error, ex.Message); } } } diff --git a/Unbinder/Program.cs b/Unbinder/Program.cs index f55395d..cb5fb90 100644 --- a/Unbinder/Program.cs +++ b/Unbinder/Program.cs @@ -47,13 +47,14 @@ builder.Services.AddCors(options => builder.Services.AddScoped(); builder.Services.AddScoped(); +// include aws service +builder.Services.AddTransient(); + +// configure front end features builder.Services.AddHttpContextAccessor(); builder.Services.AddServerSideBlazor(); - -// include extra services -//builder.Services.AddSingleton(S3Service.Client); - +// build app var app = builder.Build(); // apply most recent migration to db diff --git a/Unbinder/Services/S3Service.cs b/Unbinder/Services/S3Service.cs index f03072e..5adac6c 100644 --- a/Unbinder/Services/S3Service.cs +++ b/Unbinder/Services/S3Service.cs @@ -9,18 +9,17 @@ namespace Unbinder.Services { public sealed class S3Service { + private static AmazonS3Client CreateClient() + { + return new AmazonS3Client(Credentials, Config); + } + private static AWSCredentials Credentials { get { - string? accessKey = Environment.GetEnvironmentVariable("AWS_ACCESS_KEY"); - string? secret = Environment.GetEnvironmentVariable("AWS_SECRET_KEY"); - - if (accessKey == null || secret == null) - { - throw new Exception("AWS credentials not found"); - } - + string accessKey = Environment.GetEnvironmentVariable("AWS_ACCESS_KEY") ?? throw new Exception("AWS credentials not found"); + string secret = Environment.GetEnvironmentVariable("AWS_SECRET_KEY") ?? throw new Exception("AWS credentials not found"); return new BasicAWSCredentials(accessKey, secret); } } @@ -30,43 +29,39 @@ namespace Unbinder.Services RegionEndpoint = RegionEndpoint.USEast2 }; - private static AmazonS3Client Client => new(Credentials, Config); - private static readonly AmazonS3Client client = Client; - private static readonly string? BucketName = Environment.GetEnvironmentVariable("AWS_BUCKET_NAME"); + private static readonly string BucketName = Environment.GetEnvironmentVariable("AWS_BUCKET_NAME") ?? throw new Exception("AWS_BUCKET_NAME is not defined"); - public static async Task ListObjects(string? prefix = "") + public async Task ListObjects(string? prefix = "") { - var bucketName = BucketName; + using var _client = CreateClient(); var request = new ListObjectsRequest { - BucketName = bucketName, + BucketName = BucketName, Prefix = prefix }; - return await client.ListObjectsAsync(request); + return await _client.ListObjectsAsync(request); } public async Task UploadFileAsync(IFormFile file) { - if (file == null || file.Length == 0) - { - return null; - } + if (file == null || file.Length == 0) return null; - var bucketName = "unbinder-recipe-images"; - var keyName = file.FileName; + using var _client = CreateClient(); + using var fileTransferUtility = new TransferUtility(_client); - using var fileTransferUtility = new TransferUtility(client); - await fileTransferUtility.UploadAsync(file.OpenReadStream(), bucketName, keyName); + await fileTransferUtility.UploadAsync( + /** Stream stream, string bucketName, string key */ + file.OpenReadStream(), BucketName, file.FileName); - return keyName; + return file.FileName; } public async Task GetFile(string path, string outFile) { - var bucketName = "unbinder-recipe-images"; - using var fileTransferUtility = new TransferUtility(client); - await fileTransferUtility.DownloadAsync(outFile, bucketName, path); + using var _client = CreateClient(); + using var fileTransferUtility = new TransferUtility(_client); + await fileTransferUtility.DownloadAsync(outFile, BucketName, path); } } } diff --git a/Unbinder/Views/Home/Index.cshtml b/Unbinder/Views/Home/Index.cshtml index efc6129..c59335e 100644 --- a/Unbinder/Views/Home/Index.cshtml +++ b/Unbinder/Views/Home/Index.cshtml @@ -1,4 +1,6 @@ -@{ +@model IEnumerable? + +@{ ViewData["Title"] = "UNBINDER"; } @@ -6,4 +8,15 @@

UNBINDER

Digitize your unruly home information stores

Become u n b o u n d

+ + @if (Model != null) + { + int i = 1; + foreach (var item in Model) + { + var output = $"{i}: {item.Key.Split(".")[0]}"; +

@output

+ i++; + } + } diff --git a/Unbinder/Views/Home/Index.cshtml.cs b/Unbinder/Views/Home/Index.cshtml.cs index 42ce72f..3d28b36 100644 --- a/Unbinder/Views/Home/Index.cshtml.cs +++ b/Unbinder/Views/Home/Index.cshtml.cs @@ -1,16 +1,12 @@ -using Microsoft.AspNetCore.Mvc; +using Amazon.S3.Model; +using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; namespace Unbinder.Views.Home { - public class IndexModel : PageModel + public class IndexModel(ILogger logger) : PageModel { - private readonly ILogger _logger; - - public IndexModel(ILogger logger) - { - _logger = logger; - } + private readonly ILogger _logger = logger; public void OnGet() { diff --git a/Unbinder/Views/_ViewImports.cshtml b/Unbinder/Views/_ViewImports.cshtml index 8e54834..3847c14 100644 --- a/Unbinder/Views/_ViewImports.cshtml +++ b/Unbinder/Views/_ViewImports.cshtml @@ -2,6 +2,7 @@ @using Unbinder.Models @using Unbinder.ViewModels @using Unbinder.Controllers +@using Amazon.S3.Model @namespace Unbinder.Pages @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers