✅ AWS S3 with ASP.NET Core
Hello! So I have this code here for a minimal API that returns an image file from a S3 Bucket:
app.MapGet("images/{key}", async (string key, IAmazonS3 s3Client, IOptions<S3Settings> s3Settings) =>
{
var getRequest = new GetObjectRequest
{
BucketName = s3Settings.Value.BucketName,
Key = $"images/{key}"
};
var response = await s3Client.GetObjectAsync(getRequest);
return Results.File(response.ResponseStream, response.Headers.ContentType, response.Metadata["file-name"]);
});
What I want is to make this minimal api request into a normal one, like this here:
public async Task<ImageResponse> GetImageAsync(string fileName)
{
var getRequest = new GetObjectRequest
{
BucketName = settings.Value.BucketName,
Key = $"images/{fileName}"
};
var response = await amazonS3.GetObjectAsync(getRequest);
return new ImageResponse(response.ResponseStream, response.Headers.ContentType, response.Metadata["file-name"]);
}
My error is that I cant return the same type (in the first it is Results.File) and in the second I can't use the same and I am being forced to make a custom one. The thing is, with the second option, I get an error with "HashStream does not support seeking". How am I supposed tot deal with this?5 Replies
what is ImageResponse?
the common way is to do
return File(...)
image response is a class i made
with 3 params
a Stream param
a string for the content type
and another string for metadata
This is the error I get if I use File(): Cannot create an instance of the static class 'static class'
It is not possible to create instances of static classes. Static classes are designed to contain static fields and methods, but may not be instantiated.
i wanted to return File()
not
return new File(...)
, return File(...)
, it's a method on the controllerIt worked!
Thank you!
If you have no further questions, please use /close to mark the forum thread as answered