|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2016 Robin Appelman <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @author Robin Appelman <[email protected]> |
|
6
|
|
|
* @author Roeland Jago Douma <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* @license GNU AGPL version 3 or any later version |
|
9
|
|
|
* |
|
10
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
11
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
12
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
13
|
|
|
* License, or (at your option) any later version. |
|
14
|
|
|
* |
|
15
|
|
|
* This program is distributed in the hope that it will be useful, |
|
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
18
|
|
|
* GNU Affero General Public License for more details. |
|
19
|
|
|
* |
|
20
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
21
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
22
|
|
|
* |
|
23
|
|
|
*/ |
|
24
|
|
|
namespace OC\Files\ObjectStore; |
|
25
|
|
|
|
|
26
|
|
|
use Aws\Result; |
|
27
|
|
|
use Exception; |
|
28
|
|
|
use OCP\Files\ObjectStore\IObjectStore; |
|
29
|
|
|
use OCP\Files\ObjectStore\IObjectStoreMultiPartUpload; |
|
30
|
|
|
|
|
31
|
|
|
class S3 implements IObjectStore, IObjectStoreMultiPartUpload { |
|
32
|
|
|
use S3ConnectionTrait; |
|
33
|
|
|
use S3ObjectTrait; |
|
34
|
|
|
|
|
35
|
|
|
public function __construct($parameters) { |
|
36
|
|
|
$parameters['primary_storage'] = true; |
|
37
|
|
|
$this->parseParams($parameters); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return string the container or bucket name where objects are stored |
|
42
|
|
|
* @since 7.0.0 |
|
43
|
|
|
*/ |
|
44
|
|
|
public function getStorageId() { |
|
45
|
|
|
return $this->id; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function initiateMultipartUpload(string $urn): string { |
|
49
|
|
|
$upload = $this->getConnection()->createMultipartUpload([ |
|
50
|
|
|
'Bucket' => $this->bucket, |
|
51
|
|
|
'Key' => $urn, |
|
52
|
|
|
]); |
|
53
|
|
|
$uploadId = $upload->get('UploadId'); |
|
54
|
|
|
if ($uploadId === null) { |
|
55
|
|
|
throw new Exception('No upload id returned'); |
|
56
|
|
|
} |
|
57
|
|
|
return (string)$uploadId; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function uploadMultipartPart(string $urn, string $uploadId, int $partId, $stream, $size): Result { |
|
61
|
|
|
return $this->getConnection()->uploadPart([ |
|
62
|
|
|
'Body' => $stream, |
|
63
|
|
|
'Bucket' => $this->bucket, |
|
64
|
|
|
'Key' => $urn, |
|
65
|
|
|
'ContentLength' => $size, |
|
66
|
|
|
'PartNumber' => $partId, |
|
67
|
|
|
'UploadId' => $uploadId, |
|
68
|
|
|
]); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function getMultipartUploads(string $urn, string $uploadId): array { |
|
72
|
|
|
$parts = $this->getConnection()->listParts([ |
|
73
|
|
|
'Bucket' => $this->bucket, |
|
74
|
|
|
'Key' => $urn, |
|
75
|
|
|
'UploadId' => $uploadId, |
|
76
|
|
|
'MaxParts' => 10000 |
|
77
|
|
|
]); |
|
78
|
|
|
return $parts->get('Parts') ?? []; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function completeMultipartUpload(string $urn, string $uploadId, array $result): int { |
|
82
|
|
|
$this->getConnection()->completeMultipartUpload([ |
|
83
|
|
|
'Bucket' => $this->bucket, |
|
84
|
|
|
'Key' => $urn, |
|
85
|
|
|
'UploadId' => $uploadId, |
|
86
|
|
|
'MultipartUpload' => ['Parts' => $result], |
|
87
|
|
|
]); |
|
88
|
|
|
$stat = $this->getConnection()->headObject([ |
|
89
|
|
|
'Bucket' => $this->bucket, |
|
90
|
|
|
'Key' => $urn, |
|
91
|
|
|
]); |
|
92
|
|
|
return (int)$stat->get('ContentLength'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function abortMultipartUpload($urn, $uploadId): void { |
|
96
|
|
|
$this->getConnection()->abortMultipartUpload([ |
|
97
|
|
|
'Bucket' => $this->bucket, |
|
98
|
|
|
'Key' => $urn, |
|
99
|
|
|
'UploadId' => $uploadId |
|
100
|
|
|
]); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|