Amazon Simple Storage Service
开发人员指南 (API Version 2006-03-01)
AWS 服务或AWS文档中描述的功能,可能因地区/位置而异。点 击 Getting Started with Amazon AWS to see specific differences applicable to the China (Beijing) Region.

跟踪分段上传进度

高级别分段上传 API 提供了事件 TransferUtilityUploadRequest.UploadProgressEvent,用于在使用 TransferUtility 类上传数据时跟踪上传进度。

事件将定期发生并且将返回分段上传进度信息,例如,传输的总字节数以及事件发生时传输的字节数。

以下 C# 代码示例演示了如何订阅 UploadProgressEvent 事件和编写处理程序。

Copy
TransferUtility fileTransferUtility = new TransferUtility(new AmazonS3Client(Amazon.RegionEndpoint.USEast1)); // Use TransferUtilityUploadRequest to configure options. // In this example we subscribe to an event. TransferUtilityUploadRequest uploadRequest = new TransferUtilityUploadRequest { BucketName = existingBucketName, FilePath = filePath, Key = keyName }; 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); }

以下 C# 代码示例将文件上传到 Amazon S3 存储桶并通过订阅 TransferUtilityUploadRequest.UploadProgressEvent 事件跟踪上传进度。有关如何创建和测试有效示例的说明,请参阅 运行 Amazon S3 .NET 代码示例

Copy
using System; using System.Collections.Specialized; using System.Configuration; using Amazon.S3; using Amazon.S3.Transfer; namespace s3.amazon.com.docsamples { class TrackMPUUsingHighLevelAPI { static string existingBucketName = "*** Provide bucket name ***"; static string keyName = "*** Provide key name ***"; static string filePath = "*** Provide file to upload ***"; static void Main(string[] args) { try { TransferUtility fileTransferUtility = new TransferUtility(new AmazonS3Client(Amazon.RegionEndpoint.USEast1)); // Use TransferUtilityUploadRequest to configure options. // In this example we subscribe to an event. TransferUtilityUploadRequest uploadRequest = new TransferUtilityUploadRequest { BucketName = existingBucketName, FilePath = filePath, Key = keyName }; 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); } } }