Skip to content

Instantly share code, notes, and snippets.

@serkan-ozal
Created May 23, 2013 11:33
Show Gist options
  • Save serkan-ozal/5635454 to your computer and use it in GitHub Desktop.
Save serkan-ozal/5635454 to your computer and use it in GitHub Desktop.
public static void putObjectAsMultiPart(String bucketName, File file, long partSize) {
AmazonS3Client s3Client =
new AmazonS3Client(
new PropertiesCredentials(IOUtil.getResourceAsStream("aws-credentials.properties"));
bucketName = preProcessBucketName(bucketName);
List<PartETag> partETags = new ArrayList<PartETag>();
List<MultiPartFileUploader> uploaders = new ArrayList<MultiPartFileUploader>();
// Step 1: Initialize.
InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(bucketName, file.getName());
InitiateMultipartUploadResult initResponse = s3Client.initiateMultipartUpload(initRequest);
long contentLength = file.length();
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 upload a part.
UploadPartRequest uploadRequest =
new UploadPartRequest().
withBucketName(bucketName).withKey(file.getName()).
withUploadId(initResponse.getUploadId()).withPartNumber(i).
withFileOffset(filePosition).
withFile(file).
withPartSize(partSize);
uploadRequest.setProgressListener(new UploadProgressListener(file, i, partSize));
// Upload part and add response to our list.
MultiPartFileUploader uploader = new MultiPartFileUploader(uploadRequest);
uploaders.add(uploader);
uploader.upload();
filePosition += partSize;
}
for (MultiPartFileUploader uploader : uploaders) {
uploader.join();
partETags.add(uploader.getPartETag());
}
// Step 3: complete.
CompleteMultipartUploadRequest compRequest =
new CompleteMultipartUploadRequest(bucketName,
file.getName(),
initResponse.getUploadId(),
partETags);
s3Client.completeMultipartUpload(compRequest);
}
catch (Exception e) {
s3Client.abortMultipartUpload(
new AbortMultipartUploadRequest(bucketName, file.getName(), initResponse.getUploadId()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment