메뉴
Amazon Simple Storage Service
개발자 안내서 (API 버전 2006-03-01)

멀티파트 업로드 진행률 추적

상위 수준 Java 멀티파트 업로드 API는 객체를 Amazon S3에 업로드할 때 진행률을 추적하기 위한 수신 대기 인터페이스인 ProgressListener를 제공합니다. 진행률 이벤트는 바이트가 전송되었음을 리스너에 주기적으로 알립니다.

다음 예제에서는 ProgressEvent 이벤트 구독 및 핸들러 작성 방법을 보여 줍니다.

import java.io.File; import com.amazonaws.AmazonServiceException; import com.amazonaws.SdkClientException; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.event.ProgressEvent; import com.amazonaws.event.ProgressListener; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.PutObjectRequest; import com.amazonaws.services.s3.transfer.TransferManager; import com.amazonaws.services.s3.transfer.TransferManagerBuilder; import com.amazonaws.services.s3.transfer.Upload; public class HighLevelTrackMultipartUpload { public static void main(String[] args) throws Exception { String clientRegion = "*** Client region ***"; String bucketName = "*** Bucket name ***"; String keyName = "*** Object key ***"; String filePath = "*** Path to file to upload ***"; try { AmazonS3 s3Client = AmazonS3ClientBuilder.standard() .withRegion(clientRegion) .withCredentials(new ProfileCredentialsProvider()) .build(); TransferManager tm = TransferManagerBuilder.standard() .withS3Client(s3Client) .build(); PutObjectRequest request = new PutObjectRequest(bucketName, keyName, new File(filePath)); // To receive notifications when bytes are transferred, add a // ProgressListener to your request. request.setGeneralProgressListener(new ProgressListener() { public void progressChanged(ProgressEvent progressEvent) { System.out.println("Transferred bytes: " + progressEvent.getBytesTransferred()); } }); // TransferManager processes all transfers asynchronously, // so this call returns immediately. Upload upload = tm.upload(request); // Optionally, you can wait for the upload to finish before continuing. upload.waitForCompletion(); } catch(AmazonServiceException e) { // The call was transmitted successfully, but Amazon S3 couldn't process // it, so it returned an error response. e.printStackTrace(); } catch(SdkClientException e) { // Amazon S3 couldn't be contacted for a response, or the client // couldn't parse the response from Amazon S3. e.printStackTrace(); } } }