Sergio Tx Sergio Tx - 1 month ago 22
Java Question

Servlet reading from Amazon S3 so slow

I need a servlet to return files from Amazon S3 servers. Only the server has the credentials to access, the S3 bucket is not public. I cannot change that. I was told to use data streams, but they are so slow.
To test, I have a small proyect with thumbnails and when you click on one it opens a new tab with the full image. A 5mb image takes about a minute to load. That slow.

The function that reads from S3 and returns the data stream:

public void downloadDirectlyFromS3(String s3Path, String fileName, HttpServletResponse response) {
AmazonS3 s3Client = new AmazonS3Client(new ProfileCredentialsProvider());
s3Client.setEndpoint(S3ENDPOINT);

S3Object s3object = s3Client.getObject(new GetObjectRequest(s3Path, fileName));

byte[] buffer = new byte[5 * 1024 * 1024];

try {
InputStream input = s3object.getObjectContent();
ServletOutputStream output = response.getOutputStream();
for (int length = 0; (length = input.read(buffer)) > 0;) {
output.write(buffer, 0, length);
}
output.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

Answer

I found the answer. The problem was the logger. We are using log4j and it was set to debug, so all the trace of the stream was written in the console. Just in case it happens to somebody else, here's the link where they say it should be avoided in production: https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/java-dg-logging.html#verbose-wire-logging

And I also started using the TransferManager as saravanakumar v said, seems to be slightly faster.

Comments