Tools
What I'm trying to do
Configure the Amazon S3 client for a development environment which uses a localstack endpoint as the ServiceURL for S3
I'm trying to configure the S3 client globally in my Startup.cs
file using ASP.NET Core's native Dependency Injection
public void ConfigureServices(IServiceCollection services)
{
...
services.AddAWSService<IAmazonS3>();
}
Problem
I can't figure out how to set the ServiceURL for the S3 Client. I've seen in the AWS documentation that you can configure this property in the the appsettings.json
file. However, this wouldn't resolve my problem because I have multiple AWS services I need to integrate with.
Everything I've googled suggests you just need to do something like this:
var s3Client = new AmazonS3Client(new AmazonS3Config
{
ServiceURL = "http://localhost:5002"
});
But I would have to do this in every class that uses the S3 client, which doesn't seem right.
Any help is appreciated. Thanks
OK so I think I figured this one out.
Instead of using the built in ASP.NET Core DI:
services.AddAWSService<IAmazonS3>();
I'm using Autofac like so (docs):
var s3Config = new AmazonS3Config
{
ServiceURL = _configuration.GetSection("AWS:ServiceURL").Value,
ForcePathStyle = true
};
builder.RegisterType<AmazonS3Client>()
.As<IAmazonS3>()
.WithParameter(new TypedParameter(typeof(AmazonS3Config), s3Config))
.InstancePerLifetimeScope();
You can add a package reference to AWSSDK.Extensions.NETCore.Setup
. This give you access to some extension methods to help you setup AWS services in .NET Core DI.
services.AddDefaultAWSOptions(config.GetAWSOptions());
services.AddAWSService<IAmazonS3>();
services.AddAWSService<IAmazonSQS>();
You can then start LocalStack with the edge
service enabled and set the PORT_EDGE_HTTP
env var.
PORT_EDGE_HTTP=50234 SERVICES=edge,s3,sqs localstack start
This allows you to use the same ServiceURL
for all services offered by LocalStack instead of the various service-specific URLs running on their own ports. This can be set via appsettings.json
.
{
"AWS": {
"ServiceURL": "http://localhost:50234"
}
}
NOTE: One thing I'm still trying to figure out is how to set ForcePathStyle
via appsettings.json
. AWS doesn't need this set, but apparently LocalStack does (for now, at least).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With