수신 대기 인터페이스를 사용하여 Amazon S3에 대한 객체 업로드 진행 상황을 추적할 수 있습니다. 고급 멀티파트 업로드 API는 ProgressListener
라는 수신 대기 인터페이스를 제공합니다. 진행률 이벤트는 주기적으로 발생하며 바이트가 전송되었음을 리스너에게 통보합니다. 멀티파트 업로드에 대한 일반적인 내용을 자세히 알아보려면 Amazon S3에서 멀티파트 업로드를 사용한 객체 업로드 및 복사 섹션을 참조하세요.
추가 체크섬이 포함된 멀티파트 업로드를 사용하여 객체를 업로드하는 전체 절차는 튜토리얼: 멀티파트 업로드를 통한 객체 업로드 및 데이터 무결성 확인 섹션을 참조하세요.
다음 섹션에서는 AWS SDK와 함께 멀티파트 업로드를 추적하는 방법을 보여줍니다.
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
을(를) 사용하여 업로드 진행률을 추적합니다. 실제 예제를 작성하여 테스트하는 방법에 대한 자세한 내용은 AWS SDK for 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();
}
}
}