fix(S3): Only append streams if non-seekable

Later, when we rewind the stream in `writeMultiPart` during retry, both streams were rewinded, so the resulting stream was bigger than expected.

Inspired by https://github.com/aws/aws-sdk-php/blob/master/src/S3/ObjectUploader.php#L136-L146

Signed-off-by: Louis Chmn <louis@chmn.me>
pull/55654/head
Louis Chmn 2025-10-09 17:44:07 +07:00
parent 9e9bf4988a
commit 00ec57efd6
1 changed files with 13 additions and 1 deletions

@ -222,7 +222,19 @@ trait S3ObjectTrait {
// buffer is fully seekable, so use it directly for the small upload
$this->writeSingle($urn, $buffer, $metaData);
} else {
$loadStream = new Psr7\AppendStream([$buffer, $psrStream]);
if ($psrStream->isSeekable()) {
// If the body is seekable, just rewind the body.
$psrStream->rewind();
$loadStream = $psrStream;
} else {
// If the body is non-seekable, stitch the rewind the buffer and
// the partially read body together into one stream. This avoids
// unnecessary disk usage and does not require seeking on the
// original stream.
$buffer->rewind();
$loadStream = new Psr7\AppendStream([$buffer, $psrStream]);
}
$this->writeMultiPart($urn, $loadStream, $metaData);
}
} else {