AmazonS3。获得警告。S3AbortableInputStream:没有从S3ObjectInputStream中读取所有字节,中止了HTTP连接 您所在的位置:网站首页 java里的get AmazonS3。获得警告。S3AbortableInputStream:没有从S3ObjectInputStream中读取所有字节,中止了HTTP连接

AmazonS3。获得警告。S3AbortableInputStream:没有从S3ObjectInputStream中读取所有字节,中止了HTTP连接

#AmazonS3。获得警告。S3AbortableInputStream:没有从S3ObjectInputStream中读取所有字节,中止了HTTP连接| 来源: 网络整理| 查看: 265

百度翻译此文   有道翻译此文 问题描述

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.

推荐答案

Got the answer via other medium. Sharing it here:

The warning indicates that you called close() without reading the whole file. This is problematic because S3 is still trying to send the data and you're leaving the connection in a sad state.

There's two options here:

Read the rest of the data from the input stream so the connection can be reused. Call s3ObjectInputStream.abort() to close the connection without reading the data. The connection won't be reused, so you take some performance hit with the next request to re-create the connection. This may be worth it if it's going to take a long time to read the rest of the file. 其他推荐答案

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 } 其他推荐答案

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);


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有