Please note that with ByteBuffer you simply do manually what the AWS SDK has already done automatically for you! It still buffers the entire stream to memory, and is as good as the original solution that throws a warning from the SDK.
You can get rid of the memory problem only if you have another way to find out the length of the stream, for example, when you create a stream from a file:
void uploadFile(String bucketName, File file) { try (final InputStream stream = new FileInputStream(file)) { ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(file.length()); s3client.putObject( new PutObjectRequest(bucketName, file.getName(), stream, metadata) ); } }
Robert Jack Will
source share