简体   繁体   中英

AWS .NET SDK configure S3 Client with ASP.NET Core Dependency Injection

Tools

  • Visual Studio 2017 Professional
  • .NET Core SDK v2.2.102
  • AWSSDK.S3 v3.3.102.14

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).

For multiple AmazonS3Client , you could configure multiple IAmazonS3 like

  1. IAmazonS3

     public interface IAmazonS3A: IAmazonS3 { } public interface IAmazonS3B : IAmazonS3 { }
  2. Startup.cs

     services.AddAWSService<IAmazonS3A>(Configuration.GetAWSOptions("AWSA")); services.AddAWSService<IAmazonS3B>(Configuration.GetAWSOptions("AWSB"));
  3. appsettings.json

     { "AWSA": { "Profile": "local-test-profile", "Region": "us-west-2" }, "AWSB": { "Profile": "local-test-profile", "Region": "us-west-2" }, }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM