aws-sdk-java-v2 icon indicating copy to clipboard operation
aws-sdk-java-v2 copied to clipboard

Invalid data returned from "InputStream.read()" on an empty S3 Object

Open youngm opened this issue 3 years ago • 7 comments

Describe the bug

I have an empty S3 object. When I create an InputStream to that object and invoke "read()" I get back an int "212".

Expected Behavior

I would expect InputStream.read() to an empty S3Object to return -1.

Current Behavior

Currently it returns a single byte 212.

Reproduction Steps

When executed against a 0 byte (empty) S3Object the following code returns 212. It should return -1

      var getObjectRequest =
          GetObjectRequest.builder()
              .bucket("bucket")
              .key(
                  "emptyobject")
              .build();
      try (var fileContent =
          s3Client.getObject(getObjectRequest, ResponseTransformer.toInputStream())) {
        System.out.println(fileContent.read());
      }

Possible Solution

I don't know but the problem appears to be in the software.amazon.awssdk.services.s3.checksums.ChecksumValidatingInputStream class. That is where the result diverges from using something like InputStream.readAllBytes().

Additional Information/Context

No response

AWS Java SDK version used

2.17.283

JDK version used

17

Operating System and version

Windows 11

youngm avatar Nov 07 '22 23:11 youngm

FWIW, seems to work fine with a 1 byte object. Only empty files are a problem.

youngm avatar Nov 08 '22 00:11 youngm

Hello @youngm ,

Thank you very much for your submission and for bringing this up to our attention. I was able to reproduce and confirm the behavior with the latest version of the SDK and will bring this up to the team for review. I was wondering if you were encountering any blockers or limitation due to this?

Best regards,

Yasmine

yasminetalby avatar Nov 09 '22 21:11 yasminetalby

@yasminetalby No, I have been able to work around the issue for now on my side. Thanks for asking and picking up the issue!

youngm avatar Nov 09 '22 21:11 youngm

Of course. Thank you very much for bringing up this behavior to our attention, your feedback is very valuable to the team. I am glad to hear that you have been able to workaround the issue. I will post all updates regarding this in the comment section.

Best regards,

Yasmine

yasminetalby avatar Nov 09 '22 21:11 yasminetalby

Hello @youngm ,

Update: This bug has been added to the teams backlog. This issue submission will be updated and closed upon resolution.

Thank you very much for bringing this up to our attention.

Sincerely,

Yasmine

yasminetalby avatar Nov 16 '22 18:11 yasminetalby

Hello @yasminetalby , Is there any update about this issue? I encountered the same problem when migrating SDK from v1 to v2.

I think ChecksumValidatingInputStream.read() should immediately return -1 instead of calling inputStream.read(); when strippedLength is 0, but I'm not confident.

https://github.com/aws/aws-sdk-java-v2/blob/ea1bb9b7e725c0a1c53fe34336ae7902d3a8924b/services/s3/src/main/java/software/amazon/awssdk/services/s3/checksums/ChecksumValidatingInputStream.java#L47-L62

polyomino24 avatar Oct 06 '23 09:10 polyomino24

I've encountered this in 2.26.1 in the Trino project, we do have valid cases when we can try reading empty files.

nineinchnick avatar Jun 21 '24 15:06 nineinchnick