|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2017 Robin Appelman <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @license GNU AGPL version 3 or any later version |
|
6
|
|
|
* |
|
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
8
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
9
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
10
|
|
|
* License, or (at your option) any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
* GNU Affero General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
19
|
|
|
* |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
namespace OC\Files\ObjectStore; |
|
23
|
|
|
|
|
24
|
|
|
use Aws\Exception\MultipartUploadException; |
|
25
|
|
|
use Aws\S3\MultipartUploader; |
|
26
|
|
|
use Aws\S3\S3Client; |
|
27
|
|
|
use Psr\Http\Message\StreamInterface; |
|
28
|
|
|
|
|
29
|
|
|
const S3_UPLOAD_PART_SIZE = 524288000; // 500MB |
|
30
|
|
|
|
|
31
|
|
|
trait S3ObjectTrait { |
|
32
|
|
|
/** |
|
33
|
|
|
* Returns the connection |
|
34
|
|
|
* |
|
35
|
|
|
* @return S3Client connected client |
|
36
|
|
|
* @throws \Exception if connection could not be made |
|
37
|
|
|
*/ |
|
38
|
|
|
abstract protected function getConnection(); |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param string $urn the unified resource name used to identify the object |
|
42
|
|
|
* @return resource stream with the read data |
|
43
|
|
|
* @throws \Exception when something goes wrong, message will be logged |
|
44
|
|
|
* @since 7.0.0 |
|
45
|
|
|
*/ |
|
46
|
|
|
function readObject($urn) { |
|
|
|
|
|
|
47
|
|
|
$client = $this->getConnection(); |
|
48
|
|
|
$command = $client->getCommand('GetObject', [ |
|
49
|
|
|
'Bucket' => $this->bucket, |
|
|
|
|
|
|
50
|
|
|
'Key' => $urn |
|
51
|
|
|
]); |
|
52
|
|
|
$command['@http']['stream'] = true; |
|
53
|
|
|
$result = $client->execute($command); |
|
54
|
|
|
/** @var StreamInterface $body */ |
|
55
|
|
|
$body = $result['Body']; |
|
56
|
|
|
|
|
57
|
|
|
return $body->detach(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param string $urn the unified resource name used to identify the object |
|
62
|
|
|
* @param resource $stream stream with the data to write |
|
63
|
|
|
* @throws \Exception when something goes wrong, message will be logged |
|
64
|
|
|
* @since 7.0.0 |
|
65
|
|
|
*/ |
|
66
|
|
|
function writeObject($urn, $stream) { |
|
|
|
|
|
|
67
|
|
|
$stat = fstat($stream); |
|
68
|
|
|
|
|
69
|
|
|
if ($stat['size'] && $stat['size'] < S3_UPLOAD_PART_SIZE) { |
|
70
|
|
|
$this->singlePartUpload($urn, $stream); |
|
71
|
|
|
} else { |
|
72
|
|
|
$this->multiPartUpload($urn, $stream); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
protected function singlePartUpload($urn, $stream) { |
|
78
|
|
|
$this->getConnection()->putObject([ |
|
79
|
|
|
'Bucket' => $this->bucket, |
|
80
|
|
|
'Key' => $urn, |
|
81
|
|
|
'Body' => $stream |
|
82
|
|
|
]); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
protected function multiPartUpload($urn, $stream) { |
|
86
|
|
|
$uploader = new MultipartUploader($this->getConnection(), $stream, [ |
|
87
|
|
|
'bucket' => $this->bucket, |
|
88
|
|
|
'key' => $urn, |
|
89
|
|
|
'part_size' => S3_UPLOAD_PART_SIZE |
|
90
|
|
|
]); |
|
91
|
|
|
|
|
92
|
|
|
$tries = 0; |
|
93
|
|
|
|
|
94
|
|
|
do { |
|
95
|
|
|
try { |
|
96
|
|
|
$result = $uploader->upload(); |
|
97
|
|
|
} catch (MultipartUploadException $e) { |
|
|
|
|
|
|
98
|
|
|
\OC::$server->getLogger()->logException($e); |
|
99
|
|
|
rewind($stream); |
|
100
|
|
|
$tries++; |
|
101
|
|
|
|
|
102
|
|
|
if ($tries < 5) { |
|
103
|
|
|
$uploader = new MultipartUploader($this->getConnection(), $stream, [ |
|
104
|
|
|
'state' => $e->getState() |
|
105
|
|
|
]); |
|
106
|
|
|
} else { |
|
107
|
|
|
$this->getConnection()->abortMultipartUpload($e->getState()->getId()); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
} while (!isset($result) && $tries < 5); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param string $urn the unified resource name used to identify the object |
|
115
|
|
|
* @return void |
|
116
|
|
|
* @throws \Exception when something goes wrong, message will be logged |
|
117
|
|
|
* @since 7.0.0 |
|
118
|
|
|
*/ |
|
119
|
|
|
function deleteObject($urn) { |
|
|
|
|
|
|
120
|
|
|
$this->getConnection()->deleteObject([ |
|
121
|
|
|
'Bucket' => $this->bucket, |
|
122
|
|
|
'Key' => $urn |
|
123
|
|
|
]); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.