C
C#4mo ago
Jake

Configuring LocalStack with AWS S3 dotnet web api

So im trying to setup AmazonS3 with one of my APIs, the endpoint essentially is supposed to upload an image to localstack thats inside a docker container. My issue is with the configuration. I was searching for a resolution and apparently AWS SDK automatically appends the bucket name alongside the url of the endpoint that its trying to interact with. The resolution I found online on Github Issues is to configure AmazonS3Config and set ForcePathStyle to true. The issue im having is that it's not available anymore and I'm not entirely sure how else to configure it in Program.cs. Im using dotnet 7, this is what i currently have in my startup.
// AWS S3 configuration
builder.Services.AddDefaultAWSOptions(builder.Configuration.GetAWSOptions());
builder.Services.AddAWSService<IAmazonS3>();
// AWS S3 configuration
builder.Services.AddDefaultAWSOptions(builder.Configuration.GetAWSOptions());
builder.Services.AddAWSService<IAmazonS3>();
The error im having is:
System.Net.Http.HttpRequestException: Name or service not known (prlnk.cdn.localstack-aws:4566)
System.Net.Http.HttpRequestException: Name or service not known (prlnk.cdn.localstack-aws:4566)
Essentailly the issue im having is with this controller that basically craps some files and is supposed to upload them to s3
public async Task<ParkingImage> UploadToParklinkS3(List<IFormFile> files)
{
const string bucketName = "prlnk.cdn";
var bucketExists = await Amazon.S3.Util.AmazonS3Util.DoesS3BucketExistV2Async(_amazonS3, bucketName);

if(!bucketExists)
{
try
{
var request = new PutBucketRequest
{
BucketName = bucketName,
UseClientRegion = true,
};

await _amazonS3.PutBucketAsync(request);
}
catch (AmazonS3Exception ex)
{
Console.WriteLine($"Error creating bucket: '{ex.Message}'");
}
}
// more code....

}
public async Task<ParkingImage> UploadToParklinkS3(List<IFormFile> files)
{
const string bucketName = "prlnk.cdn";
var bucketExists = await Amazon.S3.Util.AmazonS3Util.DoesS3BucketExistV2Async(_amazonS3, bucketName);

if(!bucketExists)
{
try
{
var request = new PutBucketRequest
{
BucketName = bucketName,
UseClientRegion = true,
};

await _amazonS3.PutBucketAsync(request);
}
catch (AmazonS3Exception ex)
{
Console.WriteLine($"Error creating bucket: '{ex.Message}'");
}
}
// more code....

}
1 Reply
Jake
Jake4mo ago
Also, this is the config that im passing to the container, im not sure if that could be the problem
[default]
region = eu-west-2
output = json
endpoint_url = http://localstack-aws:4566
[default]
region = eu-west-2
output = json
endpoint_url = http://localstack-aws:4566