I have written the following code to upload collection strings to S3 using multipart upload (each part is greater than 5 MB), but when I download it back from S3 it contains only the strings from one part.
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class LowLevelMultipartUpload {
private List<PartETag> partETags = new ArrayList<>();
private InitiateMultipartUploadResult initResponse;
AmazonS3 s3Client; String bucketName; String keyName;
public LowLevelMultipartUpload(AmazonS3 s3Client, String bucketName, String keyName) {
this.s3Client = s3Client;
this.bucketName = bucketName;
this.keyName = keyName;
InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(bucketName, keyName);
initResponse = s3Client.initiateMultipartUpload(initRequest);
}
public void uploadPart(int partNumber, InputStream is, ObjectMetadata objectMetadata, boolean isFinalPart) throws IOException {
// Create the request to upload a part.
UploadPartRequest uploadRequest = new UploadPartRequest()
.withBucketName(bucketName)
.withKey(keyName)
.withUploadId(initResponse.getUploadId())
.withPartNumber(partNumber)
.withInputStream(is)
.withPartSize(is.available())
.withObjectMetadata(objectMetadata);
if(isFinalPart) uploadRequest.withLastPart(true);
// Upload the part and add the response's ETag to our list.
UploadPartResult uploadResult = s3Client.uploadPart(uploadRequest);
partETags.add(uploadResult.getPartETag());
}
public void completeUpload() {
// Complete the multipart upload.
CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest(bucketName, keyName,
initResponse.getUploadId(), partETags);
s3Client.completeMultipartUpload(compRequest);
}
}
I call the above code in the following order.
- Create LowLevelMultipartUpload object
- Upload parts in loop
- Call complete upload
No Issues in the above code, It is working fine.