SocketTimeoutException when trying reading from buffer reader in AWS Lambda

1.3k views Asked by At

I have an AWS lambda application that tries to read a csv file. It reads the file as :

S3Object s3Object = S3Client.getObject(new GetObjectRequest(SrcBucketName,SrcKey));

Reading of file is done line by line using :

final InputStreamReader isr = new InputStreamReader(s3Object.getContent());
final BufferedReader br = new BufferedReader(isr);
try{
     aList = br.lines().skip(1)
             .map(processLines)
             .collect(toList());
   } catch (Exception e) {

   } finally {
    isr.close();
    br.close();
   }

AWS Lambda throws intermittent socket time out issue. Full stacktrace is :

   Caused by: java.net.SocketTimeoutException: Read timed out
        at java.net.SocketInputStream.socketRead0(Native Method)
        at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
        at java.net.SocketInputStream.read(SocketInputStream.java:171)
        at java.net.SocketInputStream.read(SocketInputStream.java:141)
        at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
        at sun.security.ssl.InputRecord.readV3Record(InputRecord.java:593)
        at sun.security.ssl.InputRecord.read(InputRecord.java:532)
        at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:983)
        at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:940)
        at sun.security.ssl.AppInputStream.read(AppInputStream.java:105)
        at org.apache.http.impl.io.SessionInputBufferImpl.streamRead(SessionInputBufferImpl.java:139)
        at org.apache.http.impl.io.SessionInputBufferImpl.read(SessionInputBufferImpl.java:200)
        at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:178)
        at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:137)
        at com.amazonaws.internal.SdkFilterInputStream.read(SdkFilterInputStream.java:72)
        at com.amazonaws.event.ProgressInputStream.read(ProgressInputStream.java:180)
        at com.amazonaws.internal.SdkFilterInputStream.read(SdkFilterInputStream.java:72)
        at com.amazonaws.services.s3.internal.S3AbortableInputStream.read(S3AbortableInputStream.java:115)
        at com.amazonaws.internal.SdkFilterInputStream.read(SdkFilterInputStream.java:72)
        at com.amazonaws.internal.SdkFilterInputStream.read(SdkFilterInputStream.java:72)
        at com.amazonaws.internal.SdkFilterInputStream.read(SdkFilterInputStream.java:72)
        at com.amazonaws.event.ProgressInputStream.read(ProgressInputStream.java:180)
        at java.security.DigestInputStream.read(DigestInputStream.java:161)
        at com.amazonaws.services.s3.internal.DigestValidationInputStream.read(DigestValidationInputStream.java:59)
        at com.amazonaws.internal.SdkFilterInputStream.read(SdkFilterInputStream.java:72)
        at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
        at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
        at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
        at java.io.InputStreamReader.read(InputStreamReader.java:184)
        at java.io.BufferedReader.fill(BufferedReader.java:161)
        at java.io.BufferedReader.readLine(BufferedReader.java:324)
        at java.io.BufferedReader.readLine(BufferedReader.java:389)

The file size is just 61 KB. Is it an issue of SDK? Could you please tell me whats wrong here?

Thanks In Advance.

1

There are 1 answers

2
Pulkit Barad On

Try following:

    S3Object amazonS3Client.getObject(new GetObjectRequest(bucketName, filePath));
    ArrayList<String> fileContent = new ArrayList<String>();

    InputStream is = s3Object.getObjectContent;
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    Try {
    String line;
    while ((line=reader.readLine()) != null) {
            fileContent.add(line);
    }
    } Catch (IOException e) {
            e.printStackTrace();
    } finally{
        reader.close();
        is.close();
    }