C#C
C#10mo ago
Mihneas22

✅ 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?
Was this page helpful?