マルチパートアップロードの追跡 - Amazon Simple Storage Service

マルチパートアップロードの追跡

高レベルのマルチパートアップロード API は、Amazon S3 へのオブジェクトのアップロード時に進行状況を追跡するリッスンインターフェイス ProgressListener を備えています。進行状況に関するイベントが定期的に発生し、バイトが転送されたことをリスナーに通知します。

Java
TransferManager tm = new TransferManager(new ProfileCredentialsProvider()); PutObjectRequest request = new PutObjectRequest( existingBucketName, keyName, new File(filePath)); // Subscribe to the event and provide event handler. request.setProgressListener(new ProgressListener() { public void progressChanged(ProgressEvent event) { System.out.println("Transferred bytes: " + event.getBytesTransfered()); } });

以下の Java コードは、ファイルをアップロードし、ProgressListener を使用してアップロードの進行状況を追跡します。作業サンプルを作成およびテストする方法については、Amazon S3 Java コード例のテスト を参照してください。

import java.io.File; import com.amazonaws.AmazonClientException; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.event.ProgressEvent; import com.amazonaws.event.ProgressListener; import com.amazonaws.services.s3.model.PutObjectRequest; import com.amazonaws.services.s3.transfer.TransferManager; import com.amazonaws.services.s3.transfer.Upload; public class TrackMPUProgressUsingHighLevelAPI { public static void main(String[] args) throws Exception { String existingBucketName = "*** Provide bucket name ***"; String keyName = "*** Provide object key ***"; String filePath = "*** file to upload ***"; TransferManager tm = new TransferManager(new ProfileCredentialsProvider()); // For more advanced uploads, you can create a request object // and supply additional request parameters (ex: progress listeners, // canned ACLs, etc.) PutObjectRequest request = new PutObjectRequest( existingBucketName, keyName, new File(filePath)); // You can ask the upload for its progress, or you can // add a ProgressListener to your request to receive notifications // when bytes are transferred. request.setGeneralProgressListener(new ProgressListener() { @Override public void progressChanged(ProgressEvent progressEvent) { System.out.println("Transferred bytes: " + progressEvent.getBytesTransferred()); } }); // TransferManager processes all transfers asynchronously, // so this call will return immediately. Upload upload = tm.upload(request); try { // You can block and wait for the upload to finish upload.waitForCompletion(); } catch (AmazonClientException amazonClientException) { System.out.println("Unable to upload file, upload aborted."); amazonClientException.printStackTrace(); } } }
.NET

次の C# の例では、TransferUtility クラスを使用して S3 バケットにファイルをアップロードし、アップロードの進行状況を追跡します。この例と AWS SDK for .NET の特定のバージョンとの互換性、および作業サンプルを作成してテストする手順の詳細については、Amazon S3 .NET コード例の実行 を参照してください。

using Amazon; 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); } } }