Skip to content

Commit 04ac405

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 0413ec0 commit 04ac405

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
@@ -181,7 +181,19 @@ public function writeObject($urn, $stream, ?string $mimetype = null) {
181181
// buffer is fully seekable, so use it directly for the small upload
182182
$this->writeSingle($urn, $buffer, $mimetype);
183183
} else {
184-
$loadStream = new Psr7\AppendStream([$buffer, $psrStream]);
184+
if ($psrStream->isSeekable()) {
185+
// If the body is seekable, just rewind the body.
186+
$psrStream->rewind();
187+
$loadStream = $psrStream;
188+
} else {
189+
// If the body is non-seekable, stitch the rewind the buffer and
190+
// the partially read body together into one stream. This avoids
191+
// unnecessary disk usage and does not require seeking on the
192+
// original stream.
193+
$buffer->rewind();
194+
$loadStream = new Psr7\AppendStream([$buffer, $psrStream]);
195+
}
196+
185197
$this->writeMultiPart($urn, $loadStream, $mimetype);
186198
}
187199
} else {

0 commit comments

Comments
 (0)