My method receives a buffered reader and transforms each line in my file. However I need to upload the output of this transformation to an s3 bucket. The files are quite large so I would like to be able to stream my upload into an s3 object.
To do so, I think I need to use a multipart upload however I'm not sure I'm using it correctly as nothing seems to get uploaded.
Here is my method:
public void transform(BufferedReader reader)
{
Scanner scanner = new Scanner(reader);
String row;
List<PartETag> partETags = new ArrayList<>();
InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest("output-bucket", "test.log");
InitiateMultipartUploadResult result = amazonS3.initiateMultipartUpload(request);
while (scanner.hasNext()) {
row = scanner.nextLine();
InputStream inputStream = new ByteArrayInputStream(row.getBytes(Charset.forName("UTF-8")));
log.info(result.getUploadId());
UploadPartRequest uploadRequest = new UploadPartRequest()
.withBucketName("output-bucket")
.withKey("test.log")
.withUploadId(result.getUploadId())
.withInputStream(inputStream)
.withPartNumber(1)
.withPartSize(5 * 1024 * 1024);
partETags.add(amazonS3.uploadPart(uploadRequest).getPartETag());
}
log.info(result.getUploadId());
CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest(
"output-bucket",
"test.log",
result.getUploadId(),
partETags);
amazonS3.completeMultipartUpload(compRequest);
}