Skip to content

Commit 08a13dd

Browse files
authored
Merge pull request #8101 from nextcloud/s3-uploader-12
[12] Use S3Client::upload instead of splitting single/multipart upload ourselves
2 parents e328ced + 8d5b86c commit 08a13dd

File tree

2 files changed

+14
-48
lines changed

2 files changed

+14
-48
lines changed

lib/private/Files/ObjectStore/S3ObjectTrait.php

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -73,51 +73,10 @@ function readObject($urn) {
7373
* @since 7.0.0
7474
*/
7575
function writeObject($urn, $stream) {
76-
$stat = fstat($stream);
77-
78-
if ($stat['size'] && $stat['size'] < S3_UPLOAD_PART_SIZE) {
79-
$this->singlePartUpload($urn, $stream);
80-
} else {
81-
$this->multiPartUpload($urn, $stream);
82-
}
83-
84-
}
85-
86-
protected function singlePartUpload($urn, $stream) {
87-
$this->getConnection()->putObject([
88-
'Bucket' => $this->bucket,
89-
'Key' => $urn,
90-
'Body' => $stream
91-
]);
92-
}
93-
94-
protected function multiPartUpload($urn, $stream) {
95-
$uploader = new MultipartUploader($this->getConnection(), $stream, [
96-
'bucket' => $this->bucket,
97-
'key' => $urn,
76+
$this->getConnection()->upload($this->bucket, $urn, $stream, 'private', [
77+
'mup_threshold' => S3_UPLOAD_PART_SIZE,
9878
'part_size' => S3_UPLOAD_PART_SIZE
9979
]);
100-
101-
$tries = 0;
102-
103-
do {
104-
try {
105-
$result = $uploader->upload();
106-
} catch (MultipartUploadException $e) {
107-
\OC::$server->getLogger()->logException($e);
108-
rewind($stream);
109-
$tries++;
110-
111-
if ($tries < 5) {
112-
$uploader = new MultipartUploader($this->getConnection(), $stream, [
113-
'state' => $e->getState()
114-
]);
115-
} else {
116-
$this->getConnection()->abortMultipartUpload($e->getState()->getId());
117-
throw $e;
118-
}
119-
}
120-
} while (!isset($result) && $tries < 5);
12180
}
12281

12382
/**

tests/lib/Files/ObjectStore/S3Test.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
use OC\Files\ObjectStore\S3;
2525

2626
class MultiPartUploadS3 extends S3 {
27-
public function multiPartUpload($urn, $stream) {
28-
parent::multiPartUpload($urn, $stream);
27+
function writeObject($urn, $stream) {
28+
$this->getConnection()->upload($this->bucket, $urn, $stream, 'private', [
29+
'mup_threshold' => 1
30+
]);
2931
}
3032
}
3133

@@ -39,13 +41,18 @@ protected function getInstance() {
3941
$this->markTestSkipped('objectstore not configured for s3');
4042
}
4143

42-
return new MultiPartUploadS3($config['arguments']);
44+
return new S3($config['arguments']);
4345
}
4446

4547
public function testMultiPartUploader() {
46-
$s3 = $this->getInstance();
48+
$config = \OC::$server->getConfig()->getSystemValue('objectstore');
49+
if (!is_array($config) || $config['class'] !== 'OC\\Files\\ObjectStore\\S3') {
50+
$this->markTestSkipped('objectstore not configured for s3');
51+
}
52+
53+
$s3 = new MultiPartUploadS3($config['arguments']);
4754

48-
$s3->multiPartUpload('multiparttest', fopen(__FILE__, 'r'));
55+
$s3->writeObject('multiparttest', fopen(__FILE__, 'r'));
4956

5057
$result = $s3->readObject('multiparttest');
5158

0 commit comments

Comments
 (0)