User guide

PutObjectResponse response = state.Client.EndPutObject(asyncResult);
Console.WriteLine("Finished PutObject. Elapsed time: {0}",
(DateTime.Now - state.Start).ToString());
}
catch (AmazonS3Exception s3Exception) {
//
// Code to process exception
//
}
}
The following line of code calls BeginPutObject and specifies the above callback function. When the
PutObject operation completes, the callback function is called. In this example, the call to
BeginPutObject specifies, for the state parameter, an instance of the ClientState class defined
previously. This class embeds the Amazon S3 client as well as the time at which BeginPutObject is
called. The callback function uses the Amazon S3 client object to call the EndPutObject method to
retrieve the server response.The callback also extracts the start time for the operation and uses it to print
the time it took for the asynchronous operation to complete.
As in the previous examples, because exceptions that occur during the operation are received when
EndPutObject is called, this call is placed within a try block.
asyncResult = client.BeginPutObject(
request, CallbackWithState, new ClientState { Client = client, Start = Date
Time.Now } );
Complete Sample
The following code sample demonstrates the various patterns that you can use when calling the
asynchronous request methods.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
using Amazon;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
namespace async_aws_net
{
class ClientState
{
AmazonS3Client client;
DateTime startTime;
public AmazonS3Client Client
{
get { return client; }
set { client = value; }
}
Version v2.0.0
30
AWS SDK for .NET Developer Guide
Asynchronous API for .NET 3.5