멀티파트 업로드 추적 - 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); } } }