AWS SDK for .NET
The AWS SDK for .NET targets .NET 6+ and works seamlessly with Filebase by overriding the ServiceURL.
Install
dotnet add package AWSSDK.S3
Initialize the client
FilebaseClient.cs
using Amazon;
using Amazon.S3;
public static class FilebaseClient
{
public static AmazonS3Client Create()
{
var config = new AmazonS3Config
{
ServiceURL = "https://s3.filebase.io",
ForcePathStyle = true,
AuthenticationRegion = "auto",
};
return new AmazonS3Client(
Environment.GetEnvironmentVariable("FILEBASE_KEY"),
Environment.GetEnvironmentVariable("FILEBASE_SECRET"),
config
);
}
}
List buckets
var s3 = FilebaseClient.Create();
var response = await s3.ListBucketsAsync();
foreach (var bucket in response.Buckets)
{
Console.WriteLine(bucket.BucketName);
}
Create a bucket
await s3.PutBucketAsync(new PutBucketRequest
{
BucketName = "my-bucket",
});
// Public
await s3.PutBucketAsync(new PutBucketRequest
{
BucketName = "my-public-bucket",
CannedACL = S3CannedACL.PublicRead,
});
Upload an object
await s3.PutObjectAsync(new PutObjectRequest
{
BucketName = "my-bucket",
Key = "photo.jpg",
FilePath = "./photo.jpg",
ContentType = "image/jpeg",
Headers = { CacheControl = "public, max-age=31536000" },
});
For large files, use TransferUtility:
using Amazon.S3.Transfer;
using var transferUtility = new TransferUtility(s3);
await transferUtility.UploadAsync(new TransferUtilityUploadRequest
{
BucketName = "my-bucket",
Key = "video.mp4",
FilePath = "./video.mp4",
PartSize = 8 * 1024 * 1024, // 8 MB
});
TransferUtility switches to multipart upload automatically.
List objects
var request = new ListObjectsV2Request
{
BucketName = "my-bucket",
Prefix = "photos/",
MaxKeys = 1000,
};
ListObjectsV2Response response;
do
{
response = await s3.ListObjectsV2Async(request);
foreach (var obj in response.S3Objects)
{
Console.WriteLine($"{obj.Key} {obj.Size}");
}
request.ContinuationToken = response.NextContinuationToken;
}
while (response.IsTruncated);
Download an object
using var transferUtility = new TransferUtility(s3);
await transferUtility.DownloadAsync(new TransferUtilityDownloadRequest
{
BucketName = "my-bucket",
Key = "photo.jpg",
FilePath = "./downloaded.jpg",
});
Or stream directly:
var response = await s3.GetObjectAsync("my-bucket", "photo.jpg");
await using var stream = response.ResponseStream;
await using var fs = File.Create("./downloaded.jpg");
await stream.CopyToAsync(fs);
Delete an object
await s3.DeleteObjectAsync("my-bucket", "photo.jpg");
Pre-signed URLs
var url = s3.GetPreSignedURL(new GetPreSignedUrlRequest
{
BucketName = "my-bucket",
Key = "private.pdf",
Expires = DateTime.UtcNow.AddHours(1),
Verb = HttpVerb.GET,
Protocol = Protocol.HTTPS,
});
Console.WriteLine(url);
// Pre-signed PUT
var uploadUrl = s3.GetPreSignedURL(new GetPreSignedUrlRequest
{
BucketName = "uploads",
Key = $"user-{userId}/{filename}",
Expires = DateTime.UtcNow.AddMinutes(10),
Verb = HttpVerb.PUT,
ContentType = "image/jpeg",
});
Error handling
try
{
await s3.GetObjectMetadataAsync("my-bucket", "missing.jpg");
}
catch (Amazon.S3.AmazonS3Exception e)
when (e.StatusCode == System.Net.HttpStatusCode.NotFound)
{
// handle missing object
}