HLTrackProgressMPUDotNet

Track the Progress of a Multipart Upload to an S3 Bucket Using the AWS SDK for .NET (High-level API)

The following C# example uploads a file to an S3 bucket using the TransferUtility class, and tracks the progress of the upload. For information about the example's compatibility with a specific version of the AWS SDK for .NET and instructions for creating and testing a working sample, see Running the Amazon S3 .NET Code Examples.

using Amazon.S3;
using Amazon.S3.Transfer;
using System;
using System.Threading.Tasks;
namespace Amazon.DocSamples.S3
{
class TrackMPUUsingHighLevelAPITest
{
private const string bucketName = "*** provide the bucket name ***";
private const string keyName = "*** provide the name for the uploaded object ***";
private const string filePath = " *** provide the full path name of the file to upload **";
// Specify your bucket region (an example region is shown).
private static readonly RegionEndpoint bucketRegion = RegionEndpoint.USWest2;
private static IAmazonS3 s3Client;
public static void Main()
{
s3Client = new AmazonS3Client(bucketRegion);
TrackMPUAsync().Wait();
}
private static async Task TrackMPUAsync()
{
try
{
var fileTransferUtility = new TransferUtility(s3Client);
// Use TransferUtilityUploadRequest to configure options.
// In this example we subscribe to an event.
var uploadRequest =
new TransferUtilityUploadRequest
{
BucketName = bucketName,
FilePath = filePath,
Key = keyName
};
uploadRequest.UploadProgressEvent +=
new EventHandler<UploadProgressArgs>
(uploadRequest_UploadPartProgressEvent);
await fileTransferUtility.UploadAsync(uploadRequest);
Console.WriteLine("Upload completed");
}
catch (AmazonS3Exception e)
{
Console.WriteLine("Error encountered on server. Message:'{0}' when writing an object", e.Message);
}
catch (Exception e)
{
Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message);
}
}
static void uploadRequest_UploadPartProgressEvent(object sender, UploadProgressArgs e)
{
// Process event.
Console.WriteLine("{0}/{1}", e.TransferredBytes, e.TotalBytes);
}
}
}

More Info

AWS SDK for .NET