AmazonS3: Getting warning: S3AbortableInputStream:Not all bytes were read from the S3ObjectInputStream, aborting HTTP connection

Viewed 40960

Here's the warning that I am getting:

S3AbortableInputStream:Not all bytes were read from the S3ObjectInputStream, aborting HTTP connection. This is likely an error and may result in sub-optimal behavior. Request only the bytes you need via a ranged GET or drain the input stream after use.

I tried using try with resources but S3ObjectInputStream doesn't seem to close via 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 below code and explicitly closing but that doesn't work either:

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 am only reading two lines of the file from S3 and the file has more data.

6 Answers

Following option #1 of Chirag Sejpal's answer I used the below statement to drain the S3AbortableInputStream to ensure the connection can be reused:

com.amazonaws.util.IOUtils.drainInputStream(s3ObjectInputStream);
 

I ran into the same problem and the following class helped me

@Data
@AllArgsConstructor
public class S3ObjectClosable implements Closeable {
    private final S3Object s3Object;

    @Override
    public void close() throws IOException {
        s3Object.getObjectContent().abort();
        s3Object.close();
    }
}

and now you can use without warning

try (final var s3ObjectClosable = new S3ObjectClosable(s3Client.getObject(bucket, key))) {

//same code

}

To add an example to Chirag Sejpal's answer (elaborating on option #1), the following can be used to read the rest of the data from the input stream before closing it:

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
}

I ran into the same error.

As others have pointed out, the /tmp space in lambda is limited to 512 MB. And if the lambda context is re-used for a new invocation, then the /tmp space is already half-full.

So, when reading the S3 objects and writing all the files to the /tmp directory (as I was doing), I ran out of disk space somewhere in between. Lambda exited with error, but NOT all bytes from the S3ObjectInputStream were read.

So, two things one need to keep in mind:

1) If the first execution causes the problem, be stingy with your /tmp space. We have only 512 MB

2) If the second execution causes the problem, then this could be resolved by attacking the root problem. Its not possible to delete the /tmp folder. So, delete all the files in the /tmp folder after the execution is finished.

In java, here is what I did, which successfully resolved the problem.

public String handleRequest(Map < String, String > keyValuePairs, Context lambdaContext) {
  try {
    // All work here
  } catch (Exception e) {
    logger.error("Error {}", e.toString());
    return "Error";
  } finally {
    deleteAllFilesInTmpDir();
  }
}
private void deleteAllFilesInTmpDir() {
  Path path = java.nio.file.Paths.get(File.separator, "tmp", File.separator);
  try {
    if (Files.exists(path)) {
      deleteDir(path.toFile());
      logger.info("Successfully cleaned up the tmp directory");
    }
  } catch (Exception ex) {
    logger.error("Unable to clean up the tmp directory");
  }
}
public void deleteDir(File dir) {
  File[] files = dir.listFiles();
  if (files != null) {
    for (final File file: files) {
      deleteDir(file);
    }
  }
  dir.delete();
}

This is my solution. I'm using spring boot 2.4.3

Create an amazon s3 client

AmazonS3 amazonS3Client = AmazonS3ClientBuilder
                .standard()
                .withRegion("your-region")
                .withCredentials(
                        new AWSStaticCredentialsProvider(
                            new BasicAWSCredentials("your-access-key", "your-secret-access-key")))
                .build();

Create an amazon transfer client.

TransferManager transferManagerClient = TransferManagerBuilder.standard()
                .withS3Client(amazonS3Client)
                .build();

Create a temporary file in /tmp/{your-s3-key} so that we can put the file we download in this file.

File file = new File(System.getProperty("java.io.tmpdir"), "your-s3-key"); 

try {
    file.createNewFile(); // Create temporary file
} catch (IOException e) {
    e.printStackTrace();
}

file.mkdirs();  // Create the directory of the temporary file

Then, we download the file from s3 using transfer manager client

// Note that in this line the s3 file downloaded has been transferred in to the temporary file that we created
Download download = transferManagerClient.download(
               new GetObjectRequest("your-s3-bucket-name", "your-s3-key"), file); 

// This line blocks the thread until the download is finished
download.waitForCompletion();  

Now that the s3 file has been successfully transferred into the temporary file that we created. We can get the InputStream of the temporary file.

InputStream input = new DataInputStream(new FileInputStream(file));

Because the temporary file is not needed anymore, we just delete it.

file.delete();
Related