Last active
January 25, 2017 11:36
-
-
Save Aracki/eb77cb4695159c8b1a19fb49f4a9b12a to your computer and use it in GitHub Desktop.
AWS s3 file upload in parts [java]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private File multipartToFile(MultipartFile multipartFile) { | |
File file = new File(multipartFile.getOriginalFilename()); | |
try { | |
multipartFile.transferTo(file); | |
return file; | |
} catch (IOException e) { | |
e.printStackTrace(); | |
return null; | |
} | |
} | |
private void uploadInParts(String bucketForUpload, String fullPathOfFile, ObjectMetadata metadata, MultipartFile file) { | |
// <--------------------------------------------------------------> | |
// <--------------------------------------------------------------> | |
// <--------------------------------------------------------------> | |
// Create a list of UploadPartResponse objects. You get one of these for | |
// each part uploadFiles. | |
List<PartETag> partETags = new ArrayList<>(); | |
// <--------------------------------------------------------------> | |
// Step 1: Initialize. | |
InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest( | |
bucketForUpload, fullPathOfFile, metadata); | |
InitiateMultipartUploadResult initResponse = | |
amazonS3.initiateMultipartUpload(initRequest); | |
File file1 = multipartToFile(file); | |
// if cannot convert multipartFile to File | |
if (file1 == null) { | |
log.error("Fuck!"); | |
return; | |
} | |
long contentLength = file1.length(); | |
long partSize = 5 * 1024 * 1024; // set part size to 5 MB | |
// <--------------------------------------------------------------> | |
// <--------------------------------------------------------------> | |
// <--------------------------------------------------------------> | |
try { | |
// Step 2: Upload parts. | |
long filePosition = 0; | |
for (int i = 1; filePosition < contentLength; i++) { | |
// Last part can be less than 5 MB. Adjust part size. | |
partSize = Math.min(partSize, (contentLength - filePosition)); | |
// Create request to uploadFiles a part. | |
UploadPartRequest uploadRequest = new UploadPartRequest() | |
.withBucketName(bucketForUpload).withKey(fullPathOfFile) | |
.withUploadId(initResponse.getUploadId()).withPartNumber(i) | |
.withFileOffset(filePosition) | |
.withFile(file1) | |
.withPartSize(partSize); | |
// Upload part and add response to our list. | |
partETags.add(amazonS3.uploadPart(uploadRequest).getPartETag()); | |
filePosition += partSize; | |
} | |
// Step 3: Complete. | |
CompleteMultipartUploadRequest compRequest = new | |
CompleteMultipartUploadRequest( | |
bucketForUpload, | |
fullPathOfFile, | |
initResponse.getUploadId(), | |
partETags); | |
amazonS3.completeMultipartUpload(compRequest); | |
} catch (Exception e) { | |
amazonS3.abortMultipartUpload(new AbortMultipartUploadRequest( | |
bucketForUpload, fullPathOfFile, initResponse.getUploadId())); | |
} | |
// <--------------------------------------------------------------> | |
// <--------------------------------------------------------------> | |
// <--------------------------------------------------------------> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment