This is an easy-to-use .NET client for LocalStack. The client library provides a thin wrapper around aws-sdk-net which automatically configures the target endpoints to use LocalStack for your local cloud application development.
- .NET 4.6.1 (Desktop / Server)
- .NET Standard 2.0
To make use of this library, you need to have LocalStack
installed on your local machine. In particular, the localstack
command needs to be available.
The easiest way to install LocalStack .NET Client is via nuget
:
Install-Package LocalStack.Client
Or use dotnet cli
dotnet add package LocalStack.Client
Stable | Nightly |
---|---|
This library provides a thin wrapper around aws-sdk-net.
Therefore the usage of this library is same as using AWS SDK for .NET
.
See Getting Started with the AWS SDK for .NET
This library can be used with any DI library, or it can be used as standalone.
If you do not want to use any DI library, you have to instantiate SessionStandalone
as follows.
var awsAccessKeyId = "Key Id";
var awsAccessKey = "Secret Key";
var awsSessionToken = "Token";
var regionName = "us-west-1";
var localStackHost = "localhost";
ISession session = SessionStandalone
.Init()
.WithSessionOptions(awsAccessKeyId, awsAccessKey, awsSessionToken, regionName)
.WithConfig(localStackHost)
.Create();
All parameters are optional and in case of passed null
, default values will be setted. Since its workin on local machine, the real aws credantials are not needed for awsAccessKeyId
, awsAccessKey
, awsSessionToken
parameters.
First, you need to install Microsoft.Extensions.DependencyInjection
nuget package as follows
dotnet add package Microsoft.Extensions.DependencyInjection
Register necessary dependencies to ServiceCollection
as follows
var collection = new ServiceCollection();
var awsAccessKeyId = "Key Id";
var awsAccessKey = "Secret Key";
var awsSessionToken = "Token";
var regionName = "us-west-1";
var localStackHost = "localhost";
collection
.AddScoped<ISessionOptions, SessionOptions>(provider =>
new SessionOptions(awsAccessKeyId, awsAccessKey, awsSessionToken, regionName))
.AddScoped<IConfig, Config>(provider => new Config(localStackHost))
.AddScoped<ISessionReflection, SessionReflection>()
.AddScoped<ISession, Session>();
ServiceProvider serviceProvider = collection.BuildServiceProvider();
var session = serviceProvider.GetRequiredService<ISession>();
All parameters are optional and in case of passed null
, default values will be setted. Since its workin on local machine, the real aws credantials are not needed for awsAccessKeyId
, awsAccessKey
, awsSessionToken
parameters.
For all local services supported by LocalStack, the corresponding AWSSDK packages can be use.
The following example shows how to use AWSSDK.S3 with LocalStack.NET Client
var amazonS3Client = session.CreateClient<AmazonS3Client>();
ListBucketsResponse listBucketsResponse = await amazonS3Client.ListBucketsAsync();
const string bucketName = "test-bucket-3";
if (!await AmazonS3Util.DoesS3BucketExistAsync(amazonS3Client, bucketName))
{
PutBucketResponse putBucketResponse = await amazonS3Client.PutBucketAsync(bucketName);
}
var fileTransferUtility = new TransferUtility(amazonS3Client);
await fileTransferUtility.UploadAsync("SampleData.txt", bucketName, "SampleData.txt");
GetObjectResponse getObjectResponse = await amazonS3Client.GetObjectAsync(bucketName, "SampleData.txt");
See sandbox projects for more examples.
We welcome feedback, bug reports, and pull requests!
Use these commands to get you started and test your code:
Windows
build.ps1
Linux
./build.sh
Licensed under MIT, see LICENSE for the full text.