My Profile Photo

Cory Kelly

CoryPlusPlus - Cory with classes

A blog documenting solutions to some interesting problems

Read File From S3

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;
            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 = != -1)
                if (Thread.interrupted())
                    throw new InterruptedException();
                out.write(buf, 0, count);
            return out.toByteArray();


            if (object != null)
comments powered by Disqus