Use the AWS SDK for .NET with Backblaze B2
    • Dark
      Light

    Use the AWS SDK for .NET with Backblaze B2

    • Dark
      Light

    Article Summary

    You can take advantage of Backblaze B2 Cloud Storage using the AWS SDK for .NET alongside the Backblaze S3-Compatible API.

    To use the AWS SDK for .NET with Backblaze B2, you must:

    • Use version 3.7.108.0 or greater of the AWSSDK.Core package. Older versions do not support easy configuration of a custom endpoint using the service-specific endpoint feature. 
    • Configure the AWS_ENDPOINT_URL environment variable or the endpoint_url setting in the shared AWS config file with your bucket’s S3 endpoint. Note: you must include the https:// prefix in the URL, for example: https://s3.us-west-004.backblazeb2.com.
    • Configure your application key and application key ID via one of the methods supported by the AWS SDK for .NET. Note: the Backblaze application key ID is equivalent to the AWS access key id, and the Backblaze application key itself is equivalent to the AWS secret access key.

    This example demonstrates how to use the AWS SDK for .NET with Backblaze B2:

    using Amazon.S3;
    using Amazon.S3.Model;
    namespace Backblaze_B2_Samples
    {
      public class S3_Basics
      {
        public static async Task Main(string[] args)
        {
          /*
            Create an Amazon S3 client object. The constructor looks for
            credentials in the following order:
            1. Explicitly set in the AmazonS3Client() constructor
            2. A credentials profile with the name specified by a value in AWSConfigs.AWSProfileName.
            3. A credentials profile with the name specified by the AWS_PROFILE environment variable.
            4. The [default] credentials profile.
            5. AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_SESSION_TOKEN environment variables - a
               SessionAWSCredentials object is created from them, if they're all non-empty.
            6. AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables - a BasicAWSCredentials
               object is created from them, if they're both non-empty.
            Since AWSSDK.Core 3.7.108.0, you can set AWS_ENDPOINT_URL, rather than creating an AmazonS3Config
            object with a custom ServiceURL and passing it to the AmazonS3Client constructor.
          */
          IAmazonS3 client = new AmazonS3Client();
          // Create a bucket and upload something into it
          string bucket = "dotnetv2-sdk-sample" + Guid.NewGuid();
          string key = "hello_world.txt";
          try
          {
            // The AWS SDK for .NET maps the CreateBucket operation to 'PutBucket'
            await client.PutBucketAsync(new PutBucketRequest { BucketName = bucket });
            await client.PutObjectAsync(new PutObjectRequest { BucketName = bucket,
              Key = key, ContentBody = "Hello, world!" });
            Console.WriteLine($"Successfully uploaded data to {bucket}{key}");
          }
          catch (AmazonS3Exception ex)
          {
            Console.WriteLine($"Error: {ex.Message}");
          }
        }
      }
    }

    The S3-Compatible API allows 1000’s of integrations to natively work with Backblaze B2. If you are new to the S3-Compatible API, click here. If you have issues using this SDK with Backblaze B2, let us know by emailing us at [email protected].


    Was this article helpful?