.net core inject AWS configuration

Viewed 874

I have followed most of AWS-NET-SDK however unable to find a reference for what I am looking for.

Basically I am looking for to read & inject AWS options from appsettings.json file including access key, secret key, serviceUrl & bucket name, then inject IAmazonS3 to services, and retrieve everything from required constructor like MyConstructor(IAmazonS3 awsS3Client).

Currently I am doing this by initiating two objects - BasicAWSCredentials,AmazonS3Config & AmazonS3Client in following way:

private readonly IAmazonS3 _aws3Client;

public CloudSystemService(IOptions<AwsS3Settings> awsS3Settings)
{
   _awsS3Settings = awsS3Settings.Value;
    AWSCredentials credentials = new BasicAWSCredentials(_awsS3Settings.AccessKey, _awsS3Settings.SecretKey);

    var config = new AmazonS3Config
    {
        ServiceURL = _awsS3Settings.ServiceUrl,
        RegionEndpoint = Amazon.RegionEndpoint.GetBySystemName(_awsS3Settings.Region)
     };

      _awsS3Client = new AmazonS3Client(credentials, config);
 }

Basically I want everything to be read from appsettings.json file and should be handled using:

 var awsConfig = configuration.GetAWSOptions("AwsS3Settings");

 services.AddDefaultAWSOptions(awsConfig);
 services.AddAWSService<IAmazonS3>();

And my service implementation would only look like

private readonly IAmazonS3 _aws3Client;

public CloudSystemService(IAmazonS3 awsS3Client)
{
   _aws3Client = aws3Client;
}

Everything shown in first constructor should be removed.

0 Answers
Related