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

SignerUtils#moveContentLength Opens a Stream Unnecessarily

Open RealTYPICAL opened this issue 6 months ago • 2 comments

Describe the bug

SignerUtils#moveContentLength conditionally has to read the entire contents of an input stream in case a content length header isn't there. This is problematic because:

  • Some streams can only be opened once.
  • It's a performance penalty if the stream takes time to be opened.

Expected Behavior

The stream should only be opened if the content length needs to be computed.

Current Behavior

It will open a stream and then close it for no purpose.

Reproduction Steps

AtomicBoolean opened = new AtomicBoolean(false);
s3Client.putObject(b -> b.bucket("bucket").key("key"), RequestBody.fromContentProvider(() -> {
	if (opened.getAndSet(true)) {
		throw new RuntimeException("Could only be opened once.");
	}
	return new ByteArrayInputStream("a".repeat(10).getBytes());
}, 10, "application/octet-stream"));

Possible Solution

SignerUtils#moveContentLength should take a ContentStreamProvider and only open the stream if it's necessary for computing the content length.

Additional Information/Context

No response

AWS Java SDK version used

2.26.22

JDK version used

openjdk version "21.0.1"

Operating System and version

Windows 10 and WSL2

RealTYPICAL avatar Jul 31 '24 15:07 RealTYPICAL