diff --git a/lib/private/Files/ObjectStore/S3ObjectTrait.php b/lib/private/Files/ObjectStore/S3ObjectTrait.php index 0d0260e9a7b2d..dafd31595d83f 100644 --- a/lib/private/Files/ObjectStore/S3ObjectTrait.php +++ b/lib/private/Files/ObjectStore/S3ObjectTrait.php @@ -181,7 +181,19 @@ public function writeObject($urn, $stream, ?string $mimetype = null) { // buffer is fully seekable, so use it directly for the small upload $this->writeSingle($urn, $buffer, $mimetype); } 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, $mimetype); } } else {