Recently I spent the better part of 8 hours trying to read a file from s3 and send the byte array contents to our UI for consumption and display.
Finally after a very frustrating afternoon I realized the byte array was being read and converted incorrectly. Do not use IOUtils to get the byte array.
The following code snippet worked for me
public byte[] fetchS3FileAsByteArray(String bucket, String s3FileName) throws Exception
{
S3Object object = null;
S3ObjectInputStream is = null;
try
{
AmazonS3 conn = new AmazonS3Client(basicCredentials);
object = conn.getObject(new GetObjectRequest(bucket, s3FileName));
InputStream in = object.getObjectContent();
byte[] buf = new byte[1024];
ByteArrayOutputStream out = new ByteArrayOutputStream();
int count = 0;
while ((count = in.read(buf)) != -1)
{
if (Thread.interrupted())
{
throw new InterruptedException();
}
out.write(buf, 0, count);
}
out.close();
in.close();
return out.toByteArray();
}
finally
{
IOUtils.closeQuietly(is);
if (object != null)
{
object.close();
}
}
}