Sergio Tx Sergio Tx - 1 year ago 150
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());

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 = > 0;) {
output.write(buffer, 0, length);
} catch (FileNotFoundException e) {
} catch (IOException e) {

Answer Source

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:

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download