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();
}
}