AWS SDK for .NET With Wasabi
    • 12 Aug 2024
    • 12 Minutes to read
    • PDF

    AWS SDK for .NET With Wasabi

    • PDF

    Article summary

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

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

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

    1) Make use of Nuget as the package manager as shown here

    >2) Install the AWS SDK for C# or .NET

    3) Configure additional AWS CLI profile for Wasabi account using the Wasabi keys (optional)

    In this example, 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. This example shows: >

    Other examples can be referred from the AWS documentation.   

    Note that this example discusses the use of Wasabi's us-east-1 storage region. To use other Wasabi storage regions, please use the appropriate Wasabi service URL as described in this article

    Please send all IAM requests to iam.wasabisys.com

    Setting the Credentials

    using System;
    using Amazon.IdentityManagement;
    using Amazon.Runtime.CredentialManagement;
    using Amazon.S3;
    
    namespace AWSWasabi
    {
        /**
        * Example for connecting to Wasabi using credentials.
        */
        public static class Program
        {
            public static void Main(string[] args)
            {
                // 1. this is necessary for the endpoint
                var iam_config = new AmazonIdentityManagementServiceConfig {ServiceURL = "https://iam.wasabisys.com"};
                var s3_config = new AmazonS3Config() {ServiceURL = "https://s3.wasabisys.com"};
    
                // To use access key and secret key directly, just add in the access and secret keys directly to the function call:
               
                // To use AWS credentials from the AWS .credentials file.
                var chain = new CredentialProfileStoreChain();
                if (chain.TryGetProfile("wasabi", out var basicProfile))
                {
                    // TODO: process data
                }
                else
                {
                    Console.Write("Error Invalid credentials");
                }
            }
        }
    }

    Connecting to IAM and S3 Endpoints

    IAM

    using System;
    using Amazon.IdentityManagement;
    using Amazon.Runtime.CredentialManagement;
    
    namespace AWSWasabi
    {
        /**
        * Example for connecting to Wasabi IAM endpoint.
        */
        public static class Program
        {
            public static void Main(string[] args)
            {
                // 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:
                // create iam connection with credential files and config
                // var iam = new AmazonIdentityManagementServiceClient("<access-key>", "secret-key", iam_config);
    
                // 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);
    
                    // process data
                }
                else
                {
                    Console.Write("Error Invalid credentials");
                }
            }
        }
    }

    S3

    using System;
    using Amazon.Runtime.CredentialManagement;
    using Amazon.S3;
    
    namespace AWSWasabi
    {
        /**
        * Example for connecting to Wasabi S3 endpoint.
        */
        public static class Program
        {
            public static void Main(string[] args)
            {
                // 1. this is necessary for the endpoint
                var s3Config = new AmazonS3Config() {ServiceURL = "https://s3.wasabisys.com"};
    
                // To use access key and secret key directly, just add in the access and secret keys directly to the function call:
                // create iam connection with credential files and config
                // var s3 = new AmazonS3Client("<access-key>", "secret-key", s3Config);
    
                // 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 s3 = new AmazonS3Client(basicProfile.Options.AccessKey, basicProfile.Options.SecretKey, s3Config);
    
                    // TODO: process data
                   
                }
                else
                {
                    Console.Write("Error Invalid credentials");
                }
            }
        }
    }

    Creating a User Using IAM

    using System;
    using Amazon.IdentityManagement;
    using Amazon.IdentityManagement.Model;
    using Amazon.Runtime.CredentialManagement;
    
    namespace AWSWasabi
    {
        /**
        * Example for connecting to create a user using IAM.
        */
        public static class Program
        {
            public static void Main(string[] args)
            {
                // 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:
                // create iam connection with credential files and config
                // var iam = new AmazonIdentityManagementServiceClient("<access-key>", "secret-key", iam_config);
    
                // 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.
                    iam.CreateUser(createUserRequest);
                }
                else
                {
                    Console.Write("Error Invalid credentials");
                }
            }
        }
    }

    Creating a Bucket

    using System;
    using Amazon.Runtime.CredentialManagement;
    using Amazon.S3;
    using Amazon.S3.Model;
    
    namespace AWSWasabi
    {
        /**
        * Example for connecting to Wasabi S3 create bucket.
        */
        public static class Program
        {
            public static void Main(string[] args)
            {
                // 1. this is necessary for the endpoint
                var s3Config = new AmazonS3Config() {ServiceURL = "https://s3.wasabisys.com"};
    
                // To use access key and secret key directly, just add in the access and secret keys directly to the function call:
                // create iam connection with credential files and config
                // var s3 = new AmazonS3Client("<access-key>", "secret-key", s3Config);
    
                // 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 s3 = new AmazonS3Client(basicProfile.Options.AccessKey, basicProfile.Options.SecretKey, s3Config);
                   
                    var putBucketRequest = new PutBucketRequest {BucketName = "test-c-sharp-bucket-rv"};
                    s3.PutBucket(putBucketRequest);
                }
                else
                {
                    Console.Write("Error Invalid credentials");
                }
            }
        }
    }

    Uploading an Object to the Bucket

    using System;
    using Amazon.Runtime.CredentialManagement;
    using Amazon.S3;
    using Amazon.S3.Model;
    
    namespace AWSWasabi
    {
        /**
        * Example for Wasabi S3 upload object.
        */
        public static class Program
        {
            public static void Main(string[] args)
            {
                // 1. this is necessary for the endpoint
                var s3Config = new AmazonS3Config() {ServiceURL = "https://s3.wasabisys.com"};
    
                // To use access key and secret key directly, just add in the access and secret keys directly to the function call:
                // create iam connection with credential files and config
                // var s3 = new AmazonS3Client("<access-key>", "secret-key", s3Config);
    
                // 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 s3 = new AmazonS3Client(basicProfile.Options.AccessKey, basicProfile.Options.SecretKey, s3Config);
                   
                    // set parameters
                    const string bucketName = "test-c-sharp-bucket-rv";
                    const string objectFilePath = "/Users/voletiravi/RiderProjects/AWSWasabi/AWSWasabi/Test.txt";
                    const string key = "Test.txt";
                   
                    // create a Put object request
                    var putObjectRequest = new PutObjectRequest()
                    {
                        BucketName = bucketName, Key = key,
                        FilePath = objectFilePath
                    };
                   
                    // make a call to upload object
                    s3.PutObject(putObjectRequest);
                }
                else
                {
                    Console.Write("Error Invalid credentials");
                }
            }
        }
    }

    Reading an Object From the Bucket

    using System;
    using Amazon.Runtime.CredentialManagement;
    using Amazon.S3;
    using Amazon.S3.Model;
    
    namespace AWSWasabi
    {
        /**
        * Example for Wasabi S3 read object.
        */
        public static class Program
        {
            public static void Main(string[] args)
            {
                // 1. this is necessary for the endpoint
                var s3Config = new AmazonS3Config() {ServiceURL = "https://s3.wasabisys.com"};
    
                // To use access key and secret key directly, just add in the access and secret keys directly to the function call:
                // create iam connection with credential files and config
                // var s3 = new AmazonS3Client("<access-key>", "secret-key", s3Config);
    
                // 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 s3 = new AmazonS3Client(basicProfile.Options.AccessKey, basicProfile.Options.SecretKey, s3Config);
    
                    // set parameters
                    const string bucketName = "test-c-sharp-bucket-rv";
                    const string key = "Test.txt";
    
                    // create a get object request
                    var getObjectRequest = new GetObjectRequest() {BucketName = bucketName, Key = key};
                   
                    // call a get operation
                    s3.GetObject(getObjectRequest);
                }
                else
                {
                    Console.Write("Error Invalid credentials");
                }
            }
        }
    }

    Deleting the Object From the Bucket

    using System;
    using Amazon.Runtime.CredentialManagement;
    using Amazon.S3;
    using Amazon.S3.Model;
    
    namespace AWSWasabi
    {
        /**
        * Example for Wasabi S3 delete object.
        */
        public static class Program
        {
            public static void Main(string[] args)
            {
                // 1. this is necessary for the endpoint
                var s3Config = new AmazonS3Config() {ServiceURL = "https://s3.wasabisys.com"};
    
                // To use access key and secret key directly, just add in the access and secret keys directly to the function call:
                // create iam connection with credential files and config
                // var s3 = new AmazonS3Client("<access-key>", "secret-key", s3Config);
    
                // 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 s3 = new AmazonS3Client(basicProfile.Options.AccessKey, basicProfile.Options.SecretKey, s3Config);
    
                    // set parameters
                    const string bucketName = "test-c-sharp-bucket-rv";
                    const string key = "Test.txt";
    
                    // create a delete object request
                    var deleteObjectRequest = new DeleteObjectRequest() {BucketName = bucketName, Key = key};
                   
                    // call the delete operation
                    s3.DeleteObject(deleteObjectRequest);
                }
                else
                {
                    Console.Write("Error Invalid credentials");
                }
            }
        }
    }
    
    


    ESC

    Eddy AI, facilitating knowledge discovery through conversational intelligence