Skip to content

alperunal92/localstack-dotnet-client

Repository files navigation

LocalStack .Net Core and .Net Framework Client

LocalStack

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.

Continuous integration

Table of Contents

  1. Supported Platforms
  2. Prerequisites
  3. Installation
  4. Usage
  5. Developing
  6. License

Supported Platforms

Prerequisites

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.

Installation

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
NuGet MyGet

Usage

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.

Standalone Initialization

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.

Microsoft.Extensions.DependencyInjection Initialization

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.

Create and use AWS SDK Clients

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.

Developing

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

License

Licensed under MIT, see LICENSE for the full text.

About

A lightweight .NET client for LocalStack

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published