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 Storj network.
1
using Amazon;
2
using Amazon.S3;
3
using Amazon.S3.Model;
4
using Amazon.S3.Util;
5
using System;
6
using System.Threading.Tasks;
7
8
namespace Amazon.DocSamples.S3
9
{
10
class CreateBucketTest
11
{
12
private const string bucketName = "new-filebase-bucket";
13
private static IAmazonS3 client;
14
public static void Main()
15
{
16
string accessKey = "filebase-access-key";
17
string secretKey = "filebase-secret-key";
18
AmazonS3Config config = new AmazonS3Config()
19
{
20
ServiceURL = string.Format("<https://s3.filebase.com:443>"),
21
UseHttp = true,
22
ForcePathStyle = true,
23
ProxyHost = "s3.filebase.com",
24
ProxyPort = 443
25
};
26
27
AWSCredentials creds = new BasicAWSCredentials(accessKey, secretKey);
28
AmazonS3Client s3Client = new AmazonS3Client(creds, config);
29
{
30
CreateBucketAsync().Wait();
31
}
32
33
static async Task CreateBucketAsync()
34
{
35
try
36
{
37
if (!(await AmazonS3Util.DoesS3BucketExistAsync(s3Client, bucketName)))
38
{
39
var putBucketRequest = new PutBucketRequest
40
{
41
BucketName = bucketName,
42
};
43
44
PutBucketResponse putBucketResponse = await s3Client.PutBucketAsync(putBucketRequest);
45
}
46
string bucketLocation = await FindBucketLocationAsync(s3Client);
47
}
48
catch (AmazonS3Exception e)
49
{
50
Console.WriteLine("Error encountered on server. Message:'{0}' when writing an object", e.Message);
51
}
52
catch (Exception e)
53
{
54
Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message);
55
}
56
}
57
static async Task<string> FindBucketLocationAsync(IAmazonS3 client)
58
{
59
string bucketLocation;
60
var request = new GetBucketLocationRequest()
61
{
62
BucketName = bucketName
63
};
64
GetBucketLocationResponse response = await client.GetBucketLocationAsync(request);
65
bucketLocation = response.Location.ToString();
66
return bucketLocation;
67
}
68
}
69
}
Copied!

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/to/upload: The file path to the object that you want to be uploaded
  • object-name: The desired object name once uploaded
1
using Amazon;
2
using Amazon.S3;
3
using Amazon.S3.Model;
4
using System;
5
using System.Threading.Tasks;
6
7
namespace Amazon.DocSamples.S3
8
{
9
class UploadObjectTest
10
{
11
private const string bucketName = "filebase-bucket-name";
12
private const string keyName = "object-name";
13
private const string filePath = @"/path/to/object/to/upload";
14
15
private static IAmazonS3 client;
16
17
public static void Main()
18
{
19
string accessKey = "filebase-access-key";
20
string secretKey = "filebase-secret-key";
21
AmazonS3Config config = new AmazonS3Config()
22
{
23
ServiceURL = string.Format("<https://s3.filebase.com:443>"),
24
UseHttp = true,
25
ForcePathStyle = true,
26
ProxyHost = "s3.filebase.com",
27
ProxyPort = 443
28
};
29
30
client = new AmazonS3Client(bucketRegion);
31
WritingAnObjectAsync().Wait();
32
}
33
34
static async Task WritingAnObjectAsync()
35
{
36
try
37
{
38
var putRequest1 = new PutObjectRequest
39
{
40
BucketName = bucketName,
41
Key = keyName,
42
ContentBody = "sample text"
43
};
44
45
PutObjectResponse response1 = await client.PutObjectAsync(putRequest1);
46
47
}
48
catch (AmazonS3Exception e)
49
{
50
Console.WriteLine(
51
"Error encountered ***. Message:'{0}' when writing an object"
52
, e.Message);
53
}
54
catch (Exception e)
55
{
56
Console.WriteLine(
57
"Unknown encountered on server. Message:'{0}' when writing an object"
58
, e.Message);
59
}
60
}
61
}
62
}
Copied!

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
1
using Amazon;
2
using Amazon.S3;
3
using Amazon.S3.Model;
4
using System;
5
using System.IO;
6
using System.Threading.Tasks;
7
8
namespace Amazon.DocSamples.S3
9
{
10
class GetObjectTest
11
{
12
private const string bucketName = "filebase-bucket-name";
13
private const string keyName = "object-name";
14
private static IAmazonS3 client;
15
16
public static void Main()
17
{
18
string accessKey = "filebase-access-key";
19
string secretKey = "filebase-secret-key";
20
21
AmazonS3Config config = new AmazonS3Config()
22
{
23
ServiceURL = string.Format("<https://s3.filebase.com:443>"),
24
UseHttp = true,
25
ForcePathStyle = true,
26
ProxyHost = "s3.filebase.com",
27
ProxyPort = 443
28
};
29
client = new AmazonS3Client(config);
30
ReadObjectDataAsync().Wait();
31
}
32
33
static async Task ReadObjectDataAsync()
34
{
35
string responseBody = "";
36
try
37
{
38
GetObjectRequest request = new GetObjectRequest
39
{
40
BucketName = bucketName,
41
Key = keyName
42
};
43
using (GetObjectResponse response = await client.GetObjectAsync(request))
44
using (Stream responseStream = response.ResponseStream)
45
using (StreamReader reader = new StreamReader(responseStream))
46
{
47
string title = response.Metadata["x-amz-meta-title"]; // Assume you have "title" as medata added to the object.
48
string contentType = response.Headers["Content-Type"];
49
Console.WriteLine("Object metadata, Title: {0}", title);
50
Console.WriteLine("Content type: {0}", contentType);
51
52
responseBody = reader.ReadToEnd(); // Now you process the response body.
53
}
54
}
55
catch (AmazonS3Exception e)
56
{
57
// If bucket or object does not exist
58
Console.WriteLine("Error encountered ***. Message:'{0}' when reading object", e.Message);
59
}
60
catch (Exception e)
61
{
62
Console.WriteLine("Unknown encountered on server. Message:'{0}' when reading object", e.Message);
63
}
64
}
65
}
66
}
Copied!

Delete an Object

The following code example deletes an object from the specified bucket. Replace the following values in the code to match your configuration:
  • filebase-bucket-name: The Filebase Bucket Name
  • object-name: The object to be downloaded
1
using Amazon;
2
using Amazon.S3;
3
using Amazon.S3.Model;
4
using System;
5
using System.Threading.Tasks;
6
7
namespace Amazon.DocSamples.S3
8
{
9
class DeleteObjectNonVersionedBucketTest
10
{
11
private const string bucketName = "filebase-bucket-name";
12
private const string keyName = "object-name";
13
private static IAmazonS3 client;
14
15
public static void Main()
16
{
17
18
string accessKey = "filebase-access-key";
19
string secretKey = "filebase-secret-key";
20
21
AmazonS3Config config = new AmazonS3Config()
22
{
23
ServiceURL = string.Format("<https://s3.filebase.com:443>"),
24
UseHttp = true,
25
ForcePathStyle = true,
26
ProxyHost = "s3.filebase.com",
27
ProxyPort = 443
28
};
29
client = new AmazonS3Client(config);
30
31
DeleteObjectNonVersionedBucketAsync().Wait();
32
}
33
private static async Task DeleteObjectNonVersionedBucketAsync()
34
{
35
try
36
{
37
var deleteObjectRequest = new DeleteObjectRequest
38
{
39
BucketName = bucketName,
40
Key = keyName
41
};
42
43
Console.WriteLine("Deleting an object");
44
await client.DeleteObjectAsync(deleteObjectRequest);
45
}
46
catch (AmazonS3Exception e)
47
{
48
Console.WriteLine("Error encountered on server. Message:'{0}' when deleting an object", e.Message);
49
}
50
catch (Exception e)
51
{
52
Console.WriteLine("Unknown encountered on server. Message:'{0}' when deleting an object", e.Message);
53
}
54
}
55
}
56
}
Copied!
If you have any questions, please join our Discord server, or send us an email at [email protected]