AWS SDK for .NET With Wasabi

Prev Next

How do I use AWS SDK for .NET with Wasabi?

AWS SDK for C# or .NET has been certified for use with Wasabi.

This was tested with AWS SDK version 4.

To use the C#/.NET SDK, execute the following steps.

  1. Make use of Nuget as the package manager as shown in AWS SDK for C# With Wasabi.

  2. Install the AWS SDK for .NET.

  3. Configure and additional AWS CLI profile for Wasabi account using the Wasabi keys (recommended).

  4. See the samples of code below.

In these examples, we have set the profile name as "wasabi" in the "~/.aws/credentials" file.

To help our customers use this SDK with Wasabi, we have provided examples for both IAM and S3.

Other examples can be referred from the AWS documentation.  

These examples discuss the use of Wasabi's us-east-1 storage region. To use other Wasabi storage regions, use the appropriate Wasabi service URL as described in Service URLs for Wasabi's Storage Regions.

Send all IAM requests to iam.wasabisys.com

Creating a User Using IAM

using Amazon.IdentityManagement;
using Amazon.IdentityManagement.Model;
using Amazon.Runtime.CredentialManagement;

namespace AWSWasabi
{
    public static class CreateWasabiUser
    {
        private static async Task Main()
        {
            // 1. this is necessary for the endpoint
            var iamConfig = new AmazonIdentityManagementServiceConfig { ServiceURL = "https://iam.wasabisys.com" };

            // To use access key and secret key directly, just add in the access and secret keys directly to the function call:
            // var iam = new AmazonIdentityManagementServiceClient("<access-key>", "secret-key", iamConfig);

            // To use AWS credentials from the AWS .credentials file.
            var chain = new CredentialProfileStoreChain();
            if (chain.TryGetProfile("wasabi", out var basicProfile))
            {
                // create iam connection with credential files and config
                var iam = new AmazonIdentityManagementServiceClient(basicProfile.Options.AccessKey,
                    basicProfile.Options.SecretKey, iamConfig);

                // create a CreateUser request object
                var createUserRequest = new CreateUserRequest { UserName = "c-sharp-user" };

                // call the create user function.
                await iam.CreateUserAsync(createUserRequest);
            }
            else
            {
                Console.Write("Error Invalid credentials");
            }
        }
    }
}

Creating a Bucket

using Amazon.Runtime;
using Amazon.Runtime.CredentialManagement;
using Amazon.S3;
using Amazon.S3.Model;

namespace AWSWasabi
{
    public static class CreateWasabiBucket
    {
        private static IAmazonS3 _s3Client = null!;

        private const string BucketName = "mt-aws-sdk-test";

        private static async Task Main()
        {
            // 1. Configure the S3-compatible endpoint (Wasabi) using the correct URL for your bucket's region
            var config = new AmazonS3Config
            {
                ServiceURL = "https://s3.us-east-1.wasabisys.com"
            };

            // 2. Load credentials from a named profile (replaces StoredProfileAWSCredentials)
            var chain = new CredentialProfileStoreChain();
            if (!chain.TryGetAWSCredentials("wasabi", out AWSCredentials awsCredentials))
            {
                Console.WriteLine("Could not find AWS profile 'wasabi'");
                return;
            }

            // 3. Create S3 client with credentials and config
            _s3Client = new AmazonS3Client(awsCredentials, config);

            await CreateBucket(_s3Client, BucketName);
        }
        private static async Task CreateBucket(
            IAmazonS3 client,
            string bucketName)
        {
            try
            {
                var putBucketRequest = new PutBucketRequest
                {
                    BucketName = bucketName
                };
                
                await client.PutBucketAsync(putBucketRequest);
                Console.WriteLine("Create bucket completed.");
            }
            catch (AmazonS3Exception e)
            {
                Console.WriteLine($"Error: {e.Message}");
            }
        }
    }
}

Uploading an Object to the Bucket

Wasabi currently does not support the value STREAMING-AWS4-HMAC-SHA256-PAYLOAD-TRAILER for the X-Amz-Content-Sha256 header. As a result, we recommend disabling chunked transfer encoding in applications while uploading the object using the .NET SDK by setting UseChunkEncoding = false until support is available.

using Amazon.Runtime;
using Amazon.Runtime.CredentialManagement;
using Amazon.S3;
using Amazon.S3.Model;

namespace AWSWasabi
{
    public static class UploadWasabiObject
    {
        private static IAmazonS3 _s3Client = null!;

        private const string BucketName = "mt-aws-sdk-test";
        private const string ObjectName1 = "test.txt";

        // Updated to take any object from the desktop, just adjust the file name above
        private static readonly string PathToDesktop =
            Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

        private static async Task Main()
        {
            // 1. Configure the S3-compatible endpoint (Wasabi) using the correct URL for your bucket's region
            var config = new AmazonS3Config
            {
                ServiceURL = "https://s3.us-east-1.wasabisys.com"
            };

            // 2. Load credentials from a named profile (replaces StoredProfileAWSCredentials)
            var chain = new CredentialProfileStoreChain();
            if (!chain.TryGetAWSCredentials("wasabi", out AWSCredentials awsCredentials))
            {
                Console.WriteLine("Could not find AWS profile 'wasabi'");
                return;
            }

            // 3. Create S3 client with credentials and config
            _s3Client = new AmazonS3Client(awsCredentials, config);

            // The method expects the full path, including the file name.
            var path = Path.Combine(PathToDesktop, ObjectName1);

            await UploadObjectFromFileAsync(_s3Client, BucketName, ObjectName1, path);
        }

        private static async Task UploadObjectFromFileAsync(
            IAmazonS3 client,
            string bucketName,
            string objectName,
            string filePath)
        {
            try
            {
                var putRequest = new PutObjectRequest
                {
                    BucketName = bucketName,
                    Key = objectName,
                    FilePath = filePath,
                    UseChunkEncoding = false
                };

                putRequest.Metadata.Add("x-amz-meta-title", "someTitle");

                await client.PutObjectAsync(putRequest);
                Console.WriteLine("Upload completed.");
            }
            catch (AmazonS3Exception e)
            {
                Console.WriteLine($"Error: {e.Message}");
            }
        }
    }
}

Reading an Object From the Bucket

using Amazon.Runtime;
using Amazon.Runtime.CredentialManagement;
using Amazon.S3;
using Amazon.S3.Model;

namespace AWSWasabi
{
    public static class DownloadWasabiObject
    {
        private static IAmazonS3 _s3Client = null!;

        private const string BucketName = "mt-aws-sdk-test";
        private const string ObjectName1 = "test.txt";

        private static async Task Main()
        {
            // 1. Configure the S3-compatible endpoint (Wasabi) using the correct URL for your bucket's region
            var config = new AmazonS3Config
            {
                ServiceURL = "https://s3.us-east-1.wasabisys.com"
            };

            // 2. Load credentials from a named profile (replaces StoredProfileAWSCredentials)
            var chain = new CredentialProfileStoreChain();
            if (!chain.TryGetAWSCredentials("wasabi", out AWSCredentials awsCredentials))
            {
                Console.WriteLine("Could not find AWS profile 'wasabi'");
                return;
            }

            // 3. Create S3 client with credentials and config
            _s3Client = new AmazonS3Client(awsCredentials, config);

            await GetObject(_s3Client, BucketName, ObjectName1);
        }

        private static async Task GetObject(
            IAmazonS3 client,
            string bucketName,
            string objectName)
        {
            try
            {
                var getRequest = new GetObjectRequest()
                {
                    BucketName = bucketName,
                    Key = objectName,
                };
                
                using var response = await client.GetObjectAsync(getRequest);
                
                string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                string localPath = Path.Combine(desktop, objectName);
                await using var fileStream = File.Create(localPath); 
                await response.ResponseStream.CopyToAsync(fileStream);
                
                Console.WriteLine($"Downloaded to: {localPath}");
            }
            catch (AmazonS3Exception e)
            {
                Console.WriteLine($"Error: {e.Message}");
            }
        }
    }
}

Deleting the Object From the Bucket

using Amazon.Runtime;
using Amazon.Runtime.CredentialManagement;
using Amazon.S3;
using Amazon.S3.Model;

namespace AWSWasabi
{
    public static class DeleteObjectFromWasabi
    {
        private static IAmazonS3 _s3Client = null!;

        private const string BucketName = "mt-aws-sdk-test";
        private const string ObjectName1 = "test.txt";

        private static async Task Main()
        {
            // 1. Configure the S3-compatible endpoint (Wasabi) using the correct URL for your bucket's region
            var config = new AmazonS3Config
            {
                ServiceURL = "https://s3.us-east-1.wasabisys.com"
            };

            // 2. Load credentials from a named profile (replaces StoredProfileAWSCredentials)
            var chain = new CredentialProfileStoreChain();
            if (!chain.TryGetAWSCredentials("wasabi", out AWSCredentials awsCredentials))
            {
                Console.WriteLine("Could not find AWS profile 'wasabi'");
                return;
            }

            // 3. Create S3 client with credentials and config
            _s3Client = new AmazonS3Client(awsCredentials, config);

            await DeleteObject(_s3Client, BucketName, ObjectName1);
        }

        private static async Task DeleteObject(
            IAmazonS3 client,
            string bucketName,
            string objectName)
        {
            try
            {
                var deleteRequest = new DeleteObjectRequest()
                {
                    BucketName = bucketName,
                    Key = objectName,
                };
                
                await client.DeleteObjectAsync(deleteRequest);
                Console.WriteLine("Delete object completed.");
            }
            catch (AmazonS3Exception e)
            {
                Console.WriteLine($"Error: {e.Message}");
            }
        }
    }
}