AWS SDK - .NET

Learn how to use the AWS SDK for .NET with Filebase.

What is AWS SDK - .NET?

AWS SDKs (software development kits) help simplify coding and application development by supporting and providing code objects for use with S3-compatible services. There are a variety of different AWS SDKs, each for a different coding language. This guide covers AWS SDK - .NET.

The AWS SDK for .NET includes support for the Unity game engine development framework.

This guide was written using Ubuntu 20.04 and tested using the command line interface. If you are using an Integrated Development Environment (IDE), the installation of dependencies and modules will vary.

Prerequisites:

Create a Bucket

The following code example creates a new Filebase bucket. Replace the following values in the code to match your configuration:

  • new-filebase-bucket: The Intended New Bucket Name

  • filebase-access-key: Your Filebase Access Key

  • filebase-secret-key: Your Filebase Secret Key

Bucket names must be unique across all Filebase users, be between 3 and 63 characters long, and can contain only lowercase characters, numbers, and dashes.

Buckets created through this method will be automatically created on the IPFS network.

namespace CreateBucket
{
    using System;
    using System.Threading.Tasks;
    using Amazon.S3;
    using Amazon.S3.Model;

    public class CreateBucket
    {
        public static async Task Main()
        {

            // Specify a name for the new bucket.
            const string newBucketName = "new-filebase-bucket";

             {
                string accessKey = "filebase-access-key";
                string secretKey = "filebase-secret-key";
                var config = new AmazonS3Config()
            {
                ServiceURL = string.Format("https://s3.filebase.com"),
                ForcePathStyle = true,
            };
            var client = new AmazonS3Client(accessKey, secretKey, config);
            Console.WriteLine($"\nCreating a new bucket, named: {newBucketName}.");

            await CreatingBucketAsync(client, newBucketName);

        }

        static async Task CreatingBucketAsync(IAmazonS3 client, string bucketName)
        {
            try
            {
                var putBucketRequest = new PutBucketRequest
                {
                    BucketName = bucketName,
                    UseClientRegion = true,
                };

                var putBucketResponse = await client.PutBucketAsync(putBucketRequest);

            }
            catch (AmazonS3Exception ex)
            {
                Console.WriteLine($"Error creating bucket: '{ex.Message}'");
            }
        }
    }

}
}

Upload an Object

The following code example uploads an object to the specified bucket. Replace the following values in the code to match your configuration:

  • filebase-bucket-name: The Filebase Bucket Name

  • filebase-access-key: Your Filebase Access Key

  • filebase-secret-key: Your Filebase Secret Key

  • /path/to/object: The file path to the object that you want to be uploaded

  • object-name: The desired object name once uploaded

namespace UploadObjectExample
{
    using System;
    using System.Threading.Tasks;
    using Amazon.S3;
    using Amazon.S3.Model;

    public class UploadObject
    {
        private static IAmazonS3 _s3Client;

        private const string BUCKET_NAME = "filebase-bucket-name";
        private const string OBJECT_NAME1 = "object-name";

        private static string LOCAL_PATH = "/path/to/object";

        public static async Task Main()
        {
             {
                string accessKey = "filebase-access-key";
                string secretKey = "filebase-secret-key";
                var config = new AmazonS3Config()
            {
                ServiceURL = string.Format("https://s3.filebase.com"),
                ForcePathStyle = true,
            };
            _s3Client = new AmazonS3Client(accessKey, secretKey, config);

            // The method expects the full path, including the file name.
            var path = $"{LOCAL_PATH}/{OBJECT_NAME1}";

            await UploadObjectFromFileAsync(_s3Client, BUCKET_NAME, OBJECT_NAME1, path);
        }
        static async Task UploadObjectFromFileAsync(
            IAmazonS3 client,
            string bucketName,
            string objectName,
            string filePath)
        {
            try
            {
                var putRequest = new PutObjectRequest
                {
                    BucketName = bucketName,
                    Key = objectName,
                    FilePath = filePath,
                    ContentType = "text/plain",
                };

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

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

    }
}
}

Download an Object

The following code example downloads an object from the specified bucket. Replace the following values in the code to match your configuration:

  • filebase-bucket-name: The Filebase Bucket Name

  • filebase-access-key: Your Filebase Access Key

  • filebase-secret-key: Your Filebase Secret Key

  • object-name: The object to be downloaded

  • path/to/save: Local file path to save object to

namespace GetObjectExample
{
    using System;
    using System.IO;
    using System.Threading.Tasks;
    using Amazon;
    using Amazon.S3;
    using Amazon.S3.Model;

    class GetObject
    {
        public static async Task Main()
        {
            const string bucketName = "filebase-bucket-name";
            const string keyName = "object-name";

            {
                string accessKey = "filebase-access-key";
                string secretKey = "filebase-secret-key";
                var config = new AmazonS3Config()
            {
                ServiceURL = string.Format("https://s3.filebase.com"),
                ForcePathStyle = true,
            };
            var client = new AmazonS3Client(accessKey, secretKey, config);
            await ReadObjectDataAsync(client, bucketName, keyName);
        }
        static async Task ReadObjectDataAsync(IAmazonS3 client, string bucketName, string keyName)
        {

            string responseBody = string.Empty;

            try
            {
                GetObjectRequest request = new GetObjectRequest
                {
                    BucketName = bucketName,
                    Key = keyName,
                };

                using (GetObjectResponse response = await client.GetObjectAsync(request))
                using (Stream responseStream = response.ResponseStream)
                using (StreamReader reader = new StreamReader(responseStream))
                {

                    string title = response.Metadata["x-amz-meta-title"];
                    string contentType = response.Headers["Content-Type"];

                    Console.WriteLine($"Object metadata, Title: {title}");
                    Console.WriteLine($"Content type: {contentType}");

                    // Retrieve the contents of the file.
                    responseBody = reader.ReadToEnd();


                    string filePath = $"/path/to/save/{keyName}";
                }
            }
            catch (AmazonS3Exception e)
            {
 
                Console.WriteLine($"Error: '{e.Message}'");
            }
        }
    }

    }
}

If you have any questions, please join our Discord server, or send us an email at hello@filebase.com

Last updated