s3 init
This commit is contained in:
parent
f801f35fd7
commit
af5f578f83
@ -11,6 +11,8 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="AWSSDK.Extensions.NETCore.Setup" Version="4.0.3.34" />
|
||||||
|
<PackageReference Include="AWSSDK.S3" Version="4.0.22" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.SpaProxy">
|
<PackageReference Include="Microsoft.AspNetCore.SpaProxy">
|
||||||
<Version>8.*-*</Version>
|
<Version>8.*-*</Version>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
|
|||||||
33
AWS_Practice.Server/Controllers/ImagesController.cs
Normal file
33
AWS_Practice.Server/Controllers/ImagesController.cs
Normal file
@ -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<IActionResult> 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,3 +1,7 @@
|
|||||||
|
using Amazon;
|
||||||
|
using Amazon.Runtime;
|
||||||
|
using Amazon.S3;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
// Add services to the container.
|
// Add services to the container.
|
||||||
@ -7,6 +11,14 @@ builder.Services.AddControllers();
|
|||||||
builder.Services.AddEndpointsApiExplorer();
|
builder.Services.AddEndpointsApiExplorer();
|
||||||
builder.Services.AddSwaggerGen();
|
builder.Services.AddSwaggerGen();
|
||||||
|
|
||||||
|
builder.Services.AddSingleton<IAmazonS3>(_ =>
|
||||||
|
{
|
||||||
|
var aws = builder.Configuration.GetSection("AWS");
|
||||||
|
return new AmazonS3Client(
|
||||||
|
new BasicAWSCredentials(aws["AccessKey"], aws["SecretKey"]),
|
||||||
|
RegionEndpoint.GetBySystemName(aws["Region"]));
|
||||||
|
});
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
app.UseDefaultFiles();
|
app.UseDefaultFiles();
|
||||||
|
|||||||
@ -4,5 +4,9 @@
|
|||||||
"Default": "Information",
|
"Default": "Information",
|
||||||
"Microsoft.AspNetCore": "Warning"
|
"Microsoft.AspNetCore": "Warning"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"AWS": {
|
||||||
|
"AccessKey": "",
|
||||||
|
"SecretKey": ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,5 +5,9 @@
|
|||||||
"Microsoft.AspNetCore": "Warning"
|
"Microsoft.AspNetCore": "Warning"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"AllowedHosts": "*"
|
"AllowedHosts": "*",
|
||||||
|
"AWS": {
|
||||||
|
"Region": "us-east-1",
|
||||||
|
"BucketName": "saingchildren-s3"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user