1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OpenStack\ObjectStore\v1\Models; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Promise\Promise; |
6
|
|
|
use GuzzleHttp\Psr7\LimitStream; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
use OpenStack\Common\Error\BadResponseError; |
9
|
|
|
use OpenStack\Common\Resource\AbstractResource; |
10
|
|
|
use OpenStack\Common\Resource\Creatable; |
11
|
|
|
use OpenStack\Common\Resource\Deletable; |
12
|
|
|
use OpenStack\Common\Resource\HasMetadata; |
13
|
|
|
use OpenStack\Common\Resource\Listable; |
14
|
|
|
use OpenStack\Common\Resource\Retrievable; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @property \OpenStack\ObjectStore\v1\Api $api |
18
|
|
|
*/ |
19
|
|
|
class Container extends AbstractResource implements Creatable, Deletable, Retrievable, Listable, HasMetadata |
20
|
|
|
{ |
21
|
|
|
use MetadataTrait; |
22
|
|
|
|
23
|
|
|
const METADATA_PREFIX = 'X-Container-Meta-'; |
24
|
|
|
|
25
|
|
|
/** @var int */ |
26
|
|
|
public $objectCount; |
27
|
|
|
|
28
|
|
|
/** @var int */ |
29
|
|
|
public $bytesUsed; |
30
|
|
|
|
31
|
|
|
/** @var string */ |
32
|
|
|
public $name; |
33
|
|
|
|
34
|
|
|
/** @var array */ |
35
|
|
|
public $metadata; |
36
|
|
|
|
37
|
|
|
protected $markerKey = 'name'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
5 |
|
public function populateFromResponse(ResponseInterface $response) |
43
|
|
|
{ |
44
|
5 |
|
parent::populateFromResponse($response); |
45
|
|
|
|
46
|
5 |
|
$this->objectCount = $response->getHeaderLine('X-Container-Object-Count'); |
|
|
|
|
47
|
5 |
|
$this->bytesUsed = $response->getHeaderLine('X-Container-Bytes-Used'); |
|
|
|
|
48
|
5 |
|
$this->metadata = $this->parseMetadata($response); |
49
|
5 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Retrieves a collection of object resources in the form of a generator. |
53
|
|
|
* |
54
|
|
|
* @param array $options {@see \OpenStack\ObjectStore\v1\Api::getContainer} |
55
|
|
|
* @param callable|null $mapFn Allows a function to be mapped over each element. |
56
|
|
|
* |
57
|
|
|
* @return \Generator |
58
|
|
|
*/ |
59
|
1 |
|
public function listObjects(array $options = [], callable $mapFn = null) |
60
|
|
|
{ |
61
|
1 |
|
$options = array_merge($options, ['name' => $this->name, 'format' => 'json']); |
62
|
1 |
|
return $this->model(Object::class)->enumerate($this->api->getContainer(), $options, $mapFn); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
1 |
|
public function retrieve() |
69
|
|
|
{ |
70
|
1 |
|
$response = $this->executeWithState($this->api->headContainer()); |
71
|
1 |
|
$this->populateFromResponse($response); |
72
|
1 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param array $data {@see \OpenStack\ObjectStore\v1\Api::putContainer} |
76
|
|
|
* |
77
|
|
|
* @return $this |
78
|
|
|
*/ |
79
|
3 |
|
public function create(array $data) |
80
|
|
|
{ |
81
|
3 |
|
$response = $this->execute($this->api->putContainer(), $data); |
82
|
|
|
|
83
|
3 |
|
$this->populateFromResponse($response); |
84
|
3 |
|
$this->name = $data['name']; |
85
|
|
|
|
86
|
3 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
1 |
|
public function delete() |
93
|
|
|
{ |
94
|
1 |
|
$this->executeWithState($this->api->deleteContainer()); |
95
|
1 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritdoc} |
99
|
|
|
*/ |
100
|
1 |
|
public function mergeMetadata(array $metadata) |
101
|
|
|
{ |
102
|
1 |
|
$response = $this->execute($this->api->postContainer(), ['name' => $this->name, 'metadata' => $metadata]); |
103
|
1 |
|
return $this->parseMetadata($response); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
1 |
View Code Duplication |
public function resetMetadata(array $metadata) |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
$options = [ |
112
|
1 |
|
'name' => $this->name, |
113
|
1 |
|
'removeMetadata' => [], |
114
|
1 |
|
'metadata' => $metadata, |
115
|
1 |
|
]; |
116
|
|
|
|
117
|
1 |
|
foreach ($this->getMetadata() as $key => $val) { |
118
|
1 |
|
if (!array_key_exists($key, $metadata)) { |
119
|
1 |
|
$options['removeMetadata'][$key] = 'True'; |
120
|
1 |
|
} |
121
|
1 |
|
} |
122
|
|
|
|
123
|
1 |
|
$response = $this->execute($this->api->postContainer(), $options); |
124
|
1 |
|
return $this->parseMetadata($response); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* {@inheritdoc} |
129
|
|
|
*/ |
130
|
2 |
|
public function getMetadata() |
131
|
|
|
{ |
132
|
2 |
|
$response = $this->executeWithState($this->api->headContainer()); |
133
|
2 |
|
return $this->parseMetadata($response); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Retrieves an Object and populates its `name` and `containerName` properties according to the name provided and |
138
|
|
|
* the name of this container. A HTTP call will not be executed by default - you need to call |
139
|
|
|
* {@see Object::retrieve} or {@see Object::download} on the returned Object object to do that. |
140
|
|
|
* |
141
|
|
|
* @param string $name The name of the object |
142
|
|
|
* |
143
|
|
|
* @return Object |
144
|
|
|
*/ |
145
|
4 |
|
public function getObject($name) |
146
|
|
|
{ |
147
|
4 |
|
return $this->model(Object::class, ['containerName' => $this->name, 'name' => $name]); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Identifies whether an object exists in this container. |
152
|
|
|
* |
153
|
|
|
* @param string $name The name of the object. |
154
|
|
|
* |
155
|
|
|
* @return bool TRUE if the object exists, FALSE if it does not. |
156
|
|
|
* |
157
|
|
|
* @throws BadResponseError For any other HTTP error which does not have a 404 Not Found status. |
158
|
|
|
* @throws \Exception For any other type of fatal error. |
159
|
|
|
*/ |
160
|
3 |
View Code Duplication |
public function objectExists($name) |
|
|
|
|
161
|
|
|
{ |
162
|
|
|
try { |
163
|
3 |
|
$this->getObject($name)->retrieve(); |
|
|
|
|
164
|
1 |
|
return true; |
165
|
2 |
|
} catch (BadResponseError $e) { |
166
|
2 |
|
if ($e->getResponse()->getStatusCode() === 404) { |
167
|
1 |
|
return false; |
168
|
|
|
} |
169
|
1 |
|
throw $e; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Creates a single object according to the values provided. |
175
|
|
|
* |
176
|
|
|
* @param array $data {@see \OpenStack\ObjectStore\v1\Api::putObject} |
177
|
|
|
* |
178
|
|
|
* @return Object |
179
|
|
|
*/ |
180
|
2 |
|
public function createObject(array $data) |
181
|
|
|
{ |
182
|
2 |
|
return $this->model(Object::class)->create($data + ['containerName' => $this->name]); |
|
|
|
|
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Creates a Dynamic Large Object by chunking a file into smaller segments and uploading them into a holding |
187
|
|
|
* container. When this completes, a manifest file is uploaded which references the prefix of the segments, |
188
|
|
|
* allowing concatenation when a request is executed against the manifest. |
189
|
|
|
* |
190
|
|
|
* @param array $data {@see \OpenStack\ObjectStore\v1\Api::putObject} |
191
|
|
|
* @param int $data['segmentSize'] The size in Bytes of each segment |
|
|
|
|
192
|
|
|
* @param string $data['segmentContainer'] The container to which each segment will be uploaded |
|
|
|
|
193
|
|
|
* @param string $data['segmentPrefix'] The prefix that will come before each segment. If omitted, a default |
|
|
|
|
194
|
|
|
* is used: name/timestamp/filesize |
195
|
|
|
* |
196
|
|
|
* @return Object |
197
|
|
|
*/ |
198
|
1 |
|
public function createLargeObject(array $data) |
199
|
|
|
{ |
200
|
|
|
/** @var \Psr\Http\Message\StreamInterface $stream */ |
201
|
1 |
|
$stream = $data['stream']; |
202
|
|
|
|
203
|
1 |
|
$segmentSize = isset($data['segmentSize']) ? $data['segmentSize'] : 1073741824; |
204
|
1 |
|
$segmentContainer = isset($data['segmentContainer']) ? $data['segmentContainer'] : $this->name . '_segments'; |
205
|
1 |
|
$segmentPrefix = isset($data['segmentPrefix']) |
206
|
1 |
|
? $data['segmentPrefix'] |
207
|
1 |
|
: sprintf("%s/%s/%d", $data['name'], microtime(true), $stream->getSize()); |
208
|
|
|
|
209
|
|
|
/** @var \OpenStack\ObjectStore\v1\Service $service */ |
210
|
1 |
|
$service = $this->getService(); |
211
|
1 |
|
if (!$service->containerExists($segmentContainer)) { |
212
|
1 |
|
$service->createContainer(['name' => $segmentContainer]); |
213
|
1 |
|
} |
214
|
|
|
|
215
|
1 |
|
$promises = []; |
216
|
1 |
|
$count = 0; |
217
|
|
|
|
218
|
1 |
|
while (!$stream->eof() && $count < round($stream->getSize() / $segmentSize)) { |
219
|
1 |
|
$promises[] = $this->model(Object::class)->createAsync([ |
|
|
|
|
220
|
1 |
|
'name' => sprintf("%s/%d", $segmentPrefix, ++$count), |
221
|
1 |
|
'stream' => new LimitStream($stream, $segmentSize, ($count - 1) * $segmentSize), |
222
|
1 |
|
'containerName' => $segmentContainer, |
223
|
1 |
|
]); |
224
|
1 |
|
} |
225
|
|
|
|
226
|
|
|
/** @var Promise $p */ |
227
|
1 |
|
$p = \GuzzleHttp\Promise\all($promises); |
228
|
1 |
|
$p->wait(); |
229
|
|
|
|
230
|
1 |
|
return $this->createObject([ |
231
|
1 |
|
'name' => $data['name'], |
232
|
1 |
|
'objectManifest' => sprintf("%s/%s", $segmentContainer, $segmentPrefix), |
233
|
1 |
|
]); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.