I am using AWS S3 client to download big files from S3 (something around ~600MB). But in the midst of download, download fails with errors like Socket closed
or Premature end of Content-Length delimited message body
or Data received in non-data state: 6
. Error message keeps changing from one failure to other. On little bit research, it seems that such issues comes, when AmazonS3
client gets garbage collected before inputstream is completely read and written.
https://forums.aws.amazon.com/thread.jspa?messageID=438171#
Here is what code looks like
public void retrieve(String bucket, String key, String localFile){
AmazonS3 s3Client = createNewS3Client();
S3Object object = s3Client.getObject(bucket, key);
InputStream inputStream = object.getObjectContent();
OutputStream outputStream = new FileOutputStream(localFile);
//read bytes from inputstream and write to outputstream until EOF
writeBytes(inputStream, outputStream);
inputStream.close();
outputStream.close();
}
So my question is - can s3Client
in above method be garbage collected if method writeBytes
takes longer time to finish and before it can complete and return? There are no reference to s3Client
in writeBytes
method.