mirror of
https://github.com/nextcloud/server.git
synced 2024-08-19 14:11:30 +02:00
Merge pull request #8033 from nextcloud/s3-uploader
Use S3Client::upload instead of splitting single/multipart upload ourselves
This commit is contained in:
commit
26e0c14c6d
@ -75,51 +75,10 @@ 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,
|
||||
'Body' => $stream
|
||||
]);
|
||||
}
|
||||
|
||||
protected function multiPartUpload($urn, $stream) {
|
||||
$uploader = new MultipartUploader($this->getConnection(), $stream, [
|
||||
'bucket' => $this->bucket,
|
||||
'key' => $urn,
|
||||
$this->getConnection()->upload($this->bucket, $urn, $stream, 'private', [
|
||||
'mup_threshold' => S3_UPLOAD_PART_SIZE,
|
||||
'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());
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
} while (!isset($result) && $tries < 5);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -24,8 +24,10 @@ namespace Test\Files\ObjectStore;
|
||||
use OC\Files\ObjectStore\S3;
|
||||
|
||||
class MultiPartUploadS3 extends S3 {
|
||||
public function multiPartUpload($urn, $stream) {
|
||||
parent::multiPartUpload($urn, $stream);
|
||||
function writeObject($urn, $stream) {
|
||||
$this->getConnection()->upload($this->bucket, $urn, $stream, 'private', [
|
||||
'mup_threshold' => 1
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@ -39,13 +41,18 @@ class S3Test extends ObjectStoreTest {
|
||||
$this->markTestSkipped('objectstore not configured for s3');
|
||||
}
|
||||
|
||||
return new MultiPartUploadS3($config['arguments']);
|
||||
return new S3($config['arguments']);
|
||||
}
|
||||
|
||||
public function testMultiPartUploader() {
|
||||
$s3 = $this->getInstance();
|
||||
$config = \OC::$server->getConfig()->getSystemValue('objectstore');
|
||||
if (!is_array($config) || $config['class'] !== 'OC\\Files\\ObjectStore\\S3') {
|
||||
$this->markTestSkipped('objectstore not configured for s3');
|
||||
}
|
||||
|
||||
$s3->multiPartUpload('multiparttest', fopen(__FILE__, 'r'));
|
||||
$s3 = new MultiPartUploadS3($config['arguments']);
|
||||
|
||||
$s3->writeObject('multiparttest', fopen(__FILE__, 'r'));
|
||||
|
||||
$result = $s3->readObject('multiparttest');
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user