diff options
author | Robin Appelman <robin@icewind.nl> | 2017-09-21 14:06:59 +0200 |
---|---|---|
committer | Robin Appelman <robin@icewind.nl> | 2017-09-21 14:06:59 +0200 |
commit | 4ae46d887626630b675d132db3587d06de08d35d (patch) | |
tree | a4480e0b5bb57e0c4fa3caf3fa872bc2c8e87739 | |
parent | e4e5e735db2ae1d73432a3ec1ebf1a6654f2eda8 (diff) | |
download | nextcloud-server-4ae46d887626630b675d132db3587d06de08d35d.tar.gz nextcloud-server-4ae46d887626630b675d132db3587d06de08d35d.zip |
only do multipart upload for large files
Signed-off-by: Robin Appelman <robin@icewind.nl>
-rw-r--r-- | lib/private/Files/ObjectStore/S3ObjectTrait.php | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/lib/private/Files/ObjectStore/S3ObjectTrait.php b/lib/private/Files/ObjectStore/S3ObjectTrait.php index f7d9fb305c2..6527614e8d5 100644 --- a/lib/private/Files/ObjectStore/S3ObjectTrait.php +++ b/lib/private/Files/ObjectStore/S3ObjectTrait.php @@ -26,6 +26,8 @@ use Aws\S3\MultipartUploader; use Aws\S3\S3Client; use Psr\Http\Message\StreamInterface; +const S3_UPLOAD_PART_SIZE = 5368709120; + trait S3ObjectTrait { /** * Returns the connection @@ -62,17 +64,39 @@ trait S3ObjectTrait { * @since 7.0.0 */ function writeObject($urn, $stream) { + $stat = fstat($stream); + + if ($stat['size'] && $stat['size'] < S3_UPLOAD_PART_SIZE) { + $this->singlePartUpload($urn, $stream); + } else { + $this->multiPartUpload($urn, $stream); + } + + } + + private function singlePartUpload($urn, $stream) { + $this->getConnection()->putObject([ + 'Bucket' => $this->bucket, + 'Key' => $urn, + 'Body' => $stream + ]); + } + + private function multiPartUpload($urn, $stream) { $uploader = new MultipartUploader($this->getConnection(), $stream, [ 'bucket' => $this->bucket, 'key' => $urn, ]); + $tries = 0; + do { try { $result = $uploader->upload(); } catch (MultipartUploadException $e) { rewind($stream); $tries++; + if ($tries < 5) { $uploader = new MultipartUploader($this->getConnection(), $stream, [ 'state' => $e->getState() |