34 lines
991 B
C#
34 lines
991 B
C#
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();
|
|
}
|
|
}
|
|
}
|
|
}
|