Skip to main content

ASPNET Core Setup for AWS

Example of how to configure AWS clients in ASP.NET Core project with the option to run S3 locally using minio or localstack, or to configure it to hit S3 directly.

Startup.cs
var awsOptions = Configuration.GetAWSOptions();  
var useLiveCloudServices = awsOptions.Region != null;

if (useLiveCloudServices)
{
Console.WriteLine("Using Live AWS Services");
services.AddAWSService<IAmazonS3>();
}
else
{
Console.WriteLine("Use Mock AWS Services - For Local Development");
var s3Credentials = new BasicAWSCredentials("xxxx", "xxxx");
var s3Config = new AmazonS3Config
{
ForcePathStyle = true,
ServiceURL = awsOptions.DefaultClientConfig.ServiceURL
};
services.AddSingleton<IAmazonS3>(p => new AmazonS3Client(s3Credentials, s3Config));
}

services.AddAWSService<IAmazonCognitoIdentityProvider>();
services.AddAWSService<IAmazonElasticTranscoder>();
services.AddAWSService<IAmazonTranscribeService>();
services.AddAWSService<IAmazonSimpleEmailService>();

Local Configuration

docker-compose.yml
services:
minio:
image: quay.io/minio/minio
ports:
- 9000:9000
- 9001:9001
environment:
MINIO_ROOT_USER: xxxx
MINIO_ROOT_PASSWORD: xxxx
command: server --console-address ":9001" /data

use minio (or localstack)

appsettings.Development.json
{
"AWS": {
"ServiceURL": "http://localhost:9000"
}
}

AWS Configuration

use AWS directly

appsettings.Development.json
{
"AWS": {
"Profile": "local-aws-profile",
"Region": "ap-southeast-2"
}
}

Production Configuration

use service credentials

appsettings.Development.json
{
"AWS": {
}
}