From af5f578f8358fa48b880f1a8fac581858f9d8217 Mon Sep 17 00:00:00 2001 From: saingchildren Date: Thu, 23 Apr 2026 18:01:37 +0800 Subject: [PATCH] s3 init --- .../AWS_Practice.Server.csproj | 2 ++ .../Controllers/ImagesController.cs | 33 +++++++++++++++++++ AWS_Practice.Server/Program.cs | 12 +++++++ .../appsettings.Development.json | 4 +++ AWS_Practice.Server/appsettings.json | 6 +++- 5 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 AWS_Practice.Server/Controllers/ImagesController.cs diff --git a/AWS_Practice.Server/AWS_Practice.Server.csproj b/AWS_Practice.Server/AWS_Practice.Server.csproj index 59f9269..76d4618 100644 --- a/AWS_Practice.Server/AWS_Practice.Server.csproj +++ b/AWS_Practice.Server/AWS_Practice.Server.csproj @@ -11,6 +11,8 @@ + + 8.*-* diff --git a/AWS_Practice.Server/Controllers/ImagesController.cs b/AWS_Practice.Server/Controllers/ImagesController.cs new file mode 100644 index 0000000..9686955 --- /dev/null +++ b/AWS_Practice.Server/Controllers/ImagesController.cs @@ -0,0 +1,33 @@ +using Amazon.S3; +using Microsoft.AspNetCore.Mvc; + +namespace AWS_Practice.Server.Controllers +{ + [ApiController] + [Route("api/[controller]")] + public class ImagesController : ControllerBase + { + private readonly IAmazonS3 _s3; + private readonly string _bucket; + + public ImagesController(IAmazonS3 s3, IConfiguration configuration) + { + _s3 = s3; + _bucket = configuration["AWS:BucketName"]!; + } + + [HttpGet("{*key}")] + public async Task Get(string key, CancellationToken ct) + { + try + { + var response = await _s3.GetObjectAsync(_bucket, key, ct); + return File(response.ResponseStream, response.Headers.ContentType ?? "application/octet-stream"); + } + catch (AmazonS3Exception ex) when (ex.StatusCode == System.Net.HttpStatusCode.NotFound) + { + return NotFound(); + } + } + } +} diff --git a/AWS_Practice.Server/Program.cs b/AWS_Practice.Server/Program.cs index 1da74a0..6f71354 100644 --- a/AWS_Practice.Server/Program.cs +++ b/AWS_Practice.Server/Program.cs @@ -1,3 +1,7 @@ +using Amazon; +using Amazon.Runtime; +using Amazon.S3; + var builder = WebApplication.CreateBuilder(args); // Add services to the container. @@ -7,6 +11,14 @@ builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); +builder.Services.AddSingleton(_ => +{ + var aws = builder.Configuration.GetSection("AWS"); + return new AmazonS3Client( + new BasicAWSCredentials(aws["AccessKey"], aws["SecretKey"]), + RegionEndpoint.GetBySystemName(aws["Region"])); +}); + var app = builder.Build(); app.UseDefaultFiles(); diff --git a/AWS_Practice.Server/appsettings.Development.json b/AWS_Practice.Server/appsettings.Development.json index 0c208ae..952ec69 100644 --- a/AWS_Practice.Server/appsettings.Development.json +++ b/AWS_Practice.Server/appsettings.Development.json @@ -4,5 +4,9 @@ "Default": "Information", "Microsoft.AspNetCore": "Warning" } + }, + "AWS": { + "AccessKey": "", + "SecretKey": "" } } diff --git a/AWS_Practice.Server/appsettings.json b/AWS_Practice.Server/appsettings.json index 10f68b8..cd89ad1 100644 --- a/AWS_Practice.Server/appsettings.json +++ b/AWS_Practice.Server/appsettings.json @@ -5,5 +5,9 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "AWS": { + "Region": "us-east-1", + "BucketName": "saingchildren-s3" + } }