aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private
diff options
context:
space:
mode:
authorRobin Appelman <robin@icewind.nl>2017-10-11 16:10:58 +0200
committerGitHub <noreply@github.com>2017-10-11 16:10:58 +0200
commit1a99e0dab4209717f953449dbdcd10aebdd1b568 (patch)
tree37be4ff34abb2671c6290f082cc581d9fe0c4d00 /lib/private
parent647b185c2b2dc393e3135adfe935e70253c9bef8 (diff)
parente393b3553eb5ad867b34f3fdc029a1887bcd3980 (diff)
downloadnextcloud-server-1a99e0dab4209717f953449dbdcd10aebdd1b568.tar.gz
nextcloud-server-1a99e0dab4209717f953449dbdcd10aebdd1b568.zip
Merge pull request #6602 from nextcloud/s3-multipart-upload
Add multipart upload for s3
Diffstat (limited to 'lib/private')
-rw-r--r--lib/private/Files/ObjectStore/S3ObjectTrait.php45
1 files changed, 44 insertions, 1 deletions
diff --git a/lib/private/Files/ObjectStore/S3ObjectTrait.php b/lib/private/Files/ObjectStore/S3ObjectTrait.php
index 3ba4da92b98..2cbe20d9801 100644
--- a/lib/private/Files/ObjectStore/S3ObjectTrait.php
+++ b/lib/private/Files/ObjectStore/S3ObjectTrait.php
@@ -21,9 +21,13 @@
namespace OC\Files\ObjectStore;
+use Aws\Exception\MultipartUploadException;
+use Aws\S3\MultipartUploader;
use Aws\S3\S3Client;
use Psr\Http\Message\StreamInterface;
+const S3_UPLOAD_PART_SIZE = 524288000; // 500MB
+
trait S3ObjectTrait {
/**
* Returns the connection
@@ -60,6 +64,17 @@ 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);
+ }
+
+ }
+
+ protected function singlePartUpload($urn, $stream) {
$this->getConnection()->putObject([
'Bucket' => $this->bucket,
'Key' => $urn,
@@ -67,6 +82,34 @@ trait S3ObjectTrait {
]);
}
+ protected function multiPartUpload($urn, $stream) {
+ $uploader = new MultipartUploader($this->getConnection(), $stream, [
+ 'bucket' => $this->bucket,
+ 'key' => $urn,
+ 'part_size' => S3_UPLOAD_PART_SIZE
+ ]);
+
+ $tries = 0;
+
+ do {
+ try {
+ $result = $uploader->upload();
+ } catch (MultipartUploadException $e) {
+ \OC::$server->getLogger()->logException($e);
+ rewind($stream);
+ $tries++;
+
+ if ($tries < 5) {
+ $uploader = new MultipartUploader($this->getConnection(), $stream, [
+ 'state' => $e->getState()
+ ]);
+ } else {
+ $this->getConnection()->abortMultipartUpload($e->getState()->getId());
+ }
+ }
+ } while (!isset($result) && $tries < 5);
+ }
+
/**
* @param string $urn the unified resource name used to identify the object
* @return void
@@ -79,4 +122,4 @@ trait S3ObjectTrait {
'Key' => $urn
]);
}
-} \ No newline at end of file
+}