-
[Aws SDK] CreateMultipartUploadRequestjava 2021. 4. 13. 23:43
AWS SDK for Java - 2.xx 사용한 방법입니다.
- 분할 업로드 방식
- S3는 단일 개체에 대해 최대 5GB 까지만 업로드가 가능합니다.
- MultipartUpload를 통해 하나의 파일을 여러 조각으로 나누어 전송이 가능합니다.
- 100MB 이상의 파일부터 권장합니다.
- ComplatedPart를 Collection으로 관리하여 5TB까지 part를 저장하여 전송할 수 있도록 합니다.
import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.CompleteMultipartUploadRequest; import software.amazon.awssdk.services.s3.model.CompletedMultipartUpload; import software.amazon.awssdk.services.s3.model.CompletedPart; import software.amazon.awssdk.services.s3.model.CopyObjectRequest; import software.amazon.awssdk.services.s3.model.CopyObjectResponse; import software.amazon.awssdk.services.s3.model.CreateMultipartUploadRequest; import software.amazon.awssdk.services.s3.model.CreateMultipartUploadResponse; import software.amazon.awssdk.services.s3.model.UploadPartRequest;
private void multipartS3Upload(File file, String key) { //대용량 파일 업로드 try { CreateMultipartUploadRequest createMultipartUploadRequest = CreateMultipartUploadRequest.builder() .bucket(bucket).key(key).build(); CreateMultipartUploadResponse response = amazonS3 .createMultipartUpload(createMultipartUploadRequest); String uploadId = response.uploadId(); int mB = 1024 * 1024 * 100; long parts = (file.length() / (long)mB)+1; List<CompletedPart> completedParts = new ArrayList<CompletedPart>(); for(Integer i=1; i<parts+1; i++) { // Part number must be an integer between 1 and 10000 UploadPartRequest uploadPartRequest = UploadPartRequest.builder().bucket(bucket).key(key) .uploadId(uploadId).partNumber(i).build(); String etag = amazonS3 .uploadPart(uploadPartRequest, RequestBody.fromBytes(readAccessFileOffset(file, (long)(i-1)*mB, (long)mB))) .eTag(); completedParts.add(CompletedPart.builder().partNumber(i).eTag(etag).build()); log.info("[S3Client] Completed Upload File, "+key+" # Part Number : "+i); } // Finally call completeMultipartUpload operation to tell S3 to merge all uploaded // parts and finish the multipart operation. CompletedMultipartUpload completedMultipartUpload = CompletedMultipartUpload.builder() .parts(completedParts).build(); CompleteMultipartUploadRequest completeMultipartUploadRequest = CompleteMultipartUploadRequest .builder().bucket(bucket).key(key).uploadId(uploadId) .multipartUpload(completedMultipartUpload).build(); amazonS3.completeMultipartUpload(completeMultipartUploadRequest); log.info("[S3Client] Completed Upload File " + key); }catch(Exception e) { log.error(e.getMessage()); } }
참고
docs.aws.amazon.com/ko_kr/sdk-for-java/latest/developer-guide/examples-s3-objects.html
'java' 카테고리의 다른 글
[java] 자료형 정리 (0) 2021.06.24 [FILE] 파일 분할 읽기 (0) 2021.04.21 [tibero, oracle] org.hibernate.HibernateException: (0) 2021.04.10 디자인패턴 Delegate pattern 예제 (0) 2021.04.04 org.hibernate.exception.ConstraintViolationException (0) 2021.04.01 - 분할 업로드 방식