AmazonS3: Receive warning: S3AbortableInputStream: not all bytes were read from S3ObjectInputStream, breaking the HTTP connection

Here is the warning I get:

S3AbortableInputStream: not all bytes were read from S3ObjectInputStream, interrupting the HTTP connection. This is probably a mistake and may lead to suboptimal behavior. Request only the bytes you need via GET Ranged or drain the input stream after use.

I tried using try with resources, but S3ObjectInputStream does not seem to close with this method.

 try (S3Object s3object = s3Client.getObject(new GetObjectRequest(bucket, key));
      S3ObjectInputStream s3ObjectInputStream = s3object.getObjectContent();
      BufferedReader reader = new BufferedReader(new InputStreamReader(s3ObjectInputStream, StandardCharsets.UTF_8));
    ){
  //some code here blah blah blah
 }

I also tried the code below and explicitly close, but this also does not work:

S3Object s3object = s3Client.getObject(new GetObjectRequest(bucket, key));
S3ObjectInputStream s3ObjectInputStream = s3object.getObjectContent();

try (BufferedReader reader = new BufferedReader(new InputStreamReader(s3ObjectInputStream, StandardCharsets.UTF_8));
){
     //some code here blah blah
     s3ObjectInputStream.close();
     s3object.close();
}

Any help would be appreciated.

PS: I only read two lines of the file from S3, and the file has more data.

+12
2

. :

, close(), . , S3 , .

:

  • , .
  • s3ObjectInputStream.abort(), , . , . , , .
+16

Chirag Sejpal ( # 1), , :

S3Object s3object = s3Client.getObject(new GetObjectRequest(bucket, key));

try (S3ObjectInputStream s3ObjectInputStream = s3object.getObjectContent()) {
  try {
    // Read from stream as necessary
  } catch (Exception e) {
    // Handle exceptions as necessary
  } finally {
    while (s3ObjectInputStream != null && s3ObjectInputStream.read() != -1) {
      // Read the rest of the stream
    }
  }

  // The stream will be closed automatically by the try-with-resources statement
}
-1

Source: https://habr.com/ru/post/1683477/


All Articles