The method below in my Java SpringBoot application directly streams and uploads a InputStream
to an Amazon S3 bucket using the TransferManager
, and the InputStream
is derived by the MultipartFile
. I found that the code costs so many memory and I don't know why. I also found some similar questions: High memory usage when uploading a multipart file to Amazon S3 via streaming? and Java Heap Space is insufficient to upload files on AWS S3, but nothing help. Can anyone give me some help or suggestions, thank you.
public void upload(String key, MultipartFile file) throws AmazonClientException, InterruptedException, IOException {
TransferManager tm = null;
try {
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentLength(file.getSize());
tm = TransferManagerBuilder.standard().withS3Client(s3).withMinimumUploadPartSize(5 * 1024 * 1024L).withMultipartUploadThreshold(5 * 1024 * 1024L).build();
Upload upload = tm.upload(s3Config.getBucket(), key, file.getInputStream(), objectMetadata);
upload.waitForCompletion();
} finally {
input.close();
if (tm != null) {
tm.shutdownNow();
}
}
}