Asynchronous S3 file uploads with async/await

The async/await pattern in C# really is awesome. Perhaps it is just syntactic sugar over the TPL, but it makes the code so much more beautiful and easier to read. Before async/await came around this would have been an ugly mess of BeginXXX/EndXXX. Instead, now we get to do this:

var s3 = AWSClientFactory.CreateAmazonS3Client(accessKey, secretKey);
PutObjectRequest putRequest = new PutObjectRequest()
{
    FilePath = "your-file.txt",
    Key = "whatever/you/want",
    BucketName = "your-bucket-name",
};

PutObjectResponse response = await Task
    .Factory
    .FromAsync<PutObjectRequest, PutObjectResponse>(s3.BeginPutObject, s3.EndPutObject, putRequest, null);

That is so much easier on the eyes, and that's just the wrapper around the traditional BeginXXX/EndXXX calls that the Amazon SDK gives us. If you're using the newer stuff like HttpClient, it's just one line to async goodness:

HttpResponseMessage response = await client.GetAsync("http://bing.com");

No Comments