I'm trying to download an object from S3 using the AWS Android SDK 1.0.4 or 1.0.3.
This is my code:
AmazonS3Client client = getConnection(userCredentials);
S3Object obj = client.getObject(workspaceName, objectName);
ObjectMetadata meta = obj.getObjectMetadata();
long size = meta.getContentLength();
logger.info("S3 object length: "+size);
InputStream is = new BufferedInputStream(obj.getObjectContent());
byte[] fragmentBytes = IOUtils.toByteArray(is);
logger.info("Read bytes: "+ fragmentBytes.length);
This sometimes, rarely, works. Most of the time either an "java.net.SocketException: Socket is closed" is thrown or no error is reported but only part of the object is read.
Note, that the BufferedInputStream
should not be necessary for IOUtils.toByteArray(...)
and it makes no difference if it is there or not.
The problem does not seem to occur when stepping through the code line by line in a debugger.
This happens on my Android 2.3.3 device and 3.2 device. It happens over WiFi and 3G.
Any ideas?
ps> The objects are about 250k big