Skip to content

Commit

Permalink
fixup! fixup! fixup! Refactor writeObject to only use MultipartUpload…
Browse files Browse the repository at this point in the history
… when required
  • Loading branch information
juliusknorr committed Jul 13, 2021
1 parent 420e53c commit 3e32ee6
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions lib/private/Files/ObjectStore/S3ObjectTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@
*/
namespace OC\Files\ObjectStore;

use Aws\Multipart\AbstractUploader;
use Aws\S3\Exception\S3MultipartUploadException;
use Aws\S3\MultipartUploader;
use Aws\S3\S3Client;
use GuzzleHttp\Psr7\Utils;
use OC\Files\Stream\SeekableHttpStream;
use GuzzleHttp\Psr7;
use Psr\Http\Message\StreamInterface;

trait S3ObjectTrait {
/**
Expand Down Expand Up @@ -76,10 +79,9 @@ public function readObject($urn) {
/**
* Single object put helper
* @param string $urn the unified resource name used to identify the object
* @param resource $stream stream with the data to write
* @param resource|StreamInterface $stream stream with the data to write
* @param string|null $mimetype the mimetype to set for the remove object @since 22.0.0
* @throws \Exception when something goes wrong, message will be logged
* @since 7.0.0
*/
protected function writeSingle(string $urn, $stream, string $mimetype = null) {
$this->getConnection()->putObject([
Expand All @@ -95,12 +97,12 @@ protected function writeSingle(string $urn, $stream, string $mimetype = null) {
/**
* Multipart upload helper that tries to avoid orphaned fragments in S3
* @param string $urn the unified resource name used to identify the object
* @param resource $stream stream with the data to write
* @param string|null $mimetype the mimetype to set for the remove object @since 22.0.0
* @param resource|StreamInterface $stream stream with the data to write
* @param string|null $mimetype the mimetype to set for the remove object
* @throws \Exception when something goes wrong, message will be logged
* @since 7.0.0
*/
protected function writeMultiPart(string $urn, $stream, string $mimetype = null) {
/** @var AbstractUploader $uploader */
$uploader = new MultipartUploader($this->getConnection(), $stream, [
'bucket' => $this->bucket,
'key' => $urn,
Expand Down Expand Up @@ -132,12 +134,12 @@ protected function writeMultiPart(string $urn, $stream, string $mimetype = null)
* @since 7.0.0
*/
public function writeObject($urn, $stream, string $mimetype = null) {
$psrStream = Psr7\stream_for($stream);
$psrStream = Utils::streamFor($stream);

// ($psrStream->isSeekable() && $psrStream->getSize() !== null) evaluates to true for a On-Seekable stream
// so the optimisation does not apply
$buffer = new Psr7\Stream(fopen("php://memory", "rw+"));
Psr7\Utils::copyToStream($psrStream, $buffer, MultipartUploader::PART_MIN_SIZE);
$buffer = new Psr7\Stream(fopen("php://memory", 'rwb+'));
Utils::copyToStream($psrStream, $buffer, MultipartUploader::PART_MIN_SIZE);
$buffer->seek(0);
if ($buffer->getSize() < MultipartUploader::PART_MIN_SIZE) {
// buffer is fully seekable, so use it directly for the small upload
Expand Down

0 comments on commit 3e32ee6

Please sign in to comment.