AWS SDK for C# With Wasabi

Prev Next

How do I use AWS SDK for C# with Wasabi?

AWS SDK for .NET is a framework used for C# coding and is validated for use with Wasabi. This framework can be used with Wasabi by simply pointing the endpoint to the Wasabi service URL. To use the C#/.NET SDK with Wasabi:

  1. Download JetBrains Rider IDE (you may use any IDE as per your requirements). In this example, we will be using NuGet packages.

  2. Create a project in JetBrains. Navigate to NuGet to download and install the required AWS SDK packages.

  3. Type “AWS” in the search bar to see a list of all AWS SDKs. For our use case with Wasabi S3, install the following packages:

    • AWSSDK.Core

    • AWSSDK.S3

  4. Configure an additional AWS CLI profile for Wasabi account using the Wasabi keys.

    It is optional for you to use credential files to run your code; however it is always a best practice to use such implementation wherein your credential keys are in a file stored on your local machine rather than part of your actual code.

    In this example, we set the profile name as "sahanip" in the "~/.aws/credentials" file.

    Below is sample code to upload objects to Wasabi.

    This example discusses 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.

    
    using System;
    using System.Threading.Tasks;
    using Amazon.Runtime;
    using Amazon.S3;
    using Amazon.S3.Model;
    
    namespace UploadToAWS
    {
    static class UploadObject
    {
    private static IAmazonS3 _s3Client;
    
    private const string BucketName = "download-versions-bucket";
    
    private const string ObjectName1 = "new-test-file.txt";
    
    // updated it to take any object from desktop, just adjust the file name above
    private static readonly string PathToDesktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
    
    static async Task Main()
    {
    // 1. this is necessary for the endpoint
    var config = new AmazonS3Config {ServiceURL = "https://s3.wasabisys.com"};
    
    // this will allow you to call whatever profile you have
    var credentials = new StoredProfileAWSCredentials("sahanip");
    
    // create s3 connection with credential files and config.
    _s3Client = new AmazonS3Client(credentials, config);
    
    // The method expects the full path, including the file name.
    var path = $"{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
    };
    putRequest.Metadata.Add("x-amz-meta-title", "someTitle");
    await client.PutObjectAsync(putRequest);
    }
    catch (AmazonS3Exception e)
    {
    Console.WriteLine($"Error: {e.Message}");
    }
    }
    }
    }

    Be sure to use your own BucketName and Profile Name.

  5. Build and run your code in JetBrains. The exit code 0 indicates that everything ran successfully.

    You can now see the object in your bucket.


The following is a download objects script example for this document.