Skip to content

Commit 00ec57e

Browse files
committed
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>
1 parent 9e9bf49 commit 00ec57e

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

lib/private/Files/ObjectStore/S3ObjectTrait.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,19 @@ public function writeObjectWithMetaData(string $urn, $stream, array $metaData):
222222
// buffer is fully seekable, so use it directly for the small upload
223223
$this->writeSingle($urn, $buffer, $metaData);
224224
} else {
225-
$loadStream = new Psr7\AppendStream([$buffer, $psrStream]);
225+
if ($psrStream->isSeekable()) {
226+
// If the body is seekable, just rewind the body.
227+
$psrStream->rewind();
228+
$loadStream = $psrStream;
229+
} else {
230+
// If the body is non-seekable, stitch the rewind the buffer and
231+
// the partially read body together into one stream. This avoids
232+
// unnecessary disk usage and does not require seeking on the
233+
// original stream.
234+
$buffer->rewind();
235+
$loadStream = new Psr7\AppendStream([$buffer, $psrStream]);
236+
}
237+
226238
$this->writeMultiPart($urn, $loadStream, $metaData);
227239
}
228240
} else {

0 commit comments

Comments
 (0)