| Did this page help you? Yes No Tell us about it... |
The high-level multipart upload API provides an event,
TransferUtilityUploadRequest.UploadProgressEvent, to track
the upload progress when uploading data using the
TransferUtility class.
The event occurs periodically and returns multipart upload progress information such as the total number of bytes to transfer, and the number of bytes transferred at the time event occurred.
The following C# code sample demonstrates how you can subscribe to the
UploadProgressEvent event and write a handler.
TransferUtility fileTransferUtility =
new TransferUtility(accessKeyID, secretAccessKey);
// Use TransferUtilityUploadRequest to configure options.
// In this example we subscribe to an event.
TransferUtilityUploadRequest uploadRequest =
new TransferUtilityUploadRequest()
.WithBucketName(existingBucketName)
.WithFilePath(filePath);
uploadRequest.UploadProgressEvent +=
new EventHandler<UploadProgressArgs>(uploadRequest_UploadPartProgressEvent);
fileTransferUtility.Upload(uploadRequest);
static void uploadRequest_UploadPartProgressEvent(object sender, UploadProgressArgs e)
{
// Process event.
Console.WriteLine("{0}/{1}", e.TransferredBytes, e.TotalBytes);
}Example
The following C# code example uploads a file to an Amazon S3 bucket and tracks the
progress by subscribing to the
TransferUtilityUploadRequest.UploadProgressEvent event.
For instructions on how to create and test a working sample, see
Testing the .NET Code Examples
using System;
using System.Configuration;
using Amazon.S3;
using Amazon.S3.Model;
using System.Collections.Specialized;
using Amazon.S3.Transfer;
using System.IO;
namespace s3.amazon.com.docsamples.highlevel_trackprogress
{
class Program
{
static string accessKeyID = "";
static string secretAccessKey = "";
static string existingBucketName = "*** Provide bucket name ***";
static string keyName = "*** Provide key name ***";
static string filePath = "*** Provide file to upload ***";
static void Main(string[] args)
{
NameValueCollection appConfig = ConfigurationManager.AppSettings;
accessKeyID = appConfig["AWSAccessKey"];
secretAccessKey = appConfig["AWSSecretKey"];
try
{
TransferUtility fileTransferUtility =
new TransferUtility(accessKeyID, secretAccessKey);
// Use TransferUtilityUploadRequest to configure options.
// In this example we subscribe to an event.
TransferUtilityUploadRequest uploadRequest =
new TransferUtilityUploadRequest()
.WithBucketName(existingBucketName)
.WithFilePath(filePath);
uploadRequest.UploadProgressEvent +=
new EventHandler<UploadProgressArgs>
(uploadRequest_UploadPartProgressEvent);
fileTransferUtility.Upload(uploadRequest);
Console.WriteLine("Upload completed");
}
catch (AmazonS3Exception e)
{
Console.WriteLine(e.Message, e.InnerException);
}
}
static void uploadRequest_UploadPartProgressEvent(
object sender, UploadProgressArgs e)
{
// Process event.
Console.WriteLine("{0}/{1}", e.TransferredBytes, e.TotalBytes);
}
}
}