|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Jörn Friedrich Dreyer <[email protected]> |
|
4
|
|
|
* @author Morris Jobke <[email protected]> |
|
5
|
|
|
* @author Robin Appelman <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc. |
|
8
|
|
|
* @license AGPL-3.0 |
|
9
|
|
|
* |
|
10
|
|
|
* This code is free software: you can redistribute it and/or modify |
|
11
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
|
12
|
|
|
* as published by the Free Software Foundation. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU Affero General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
|
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|
21
|
|
|
* |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace OC\Files\ObjectStore; |
|
25
|
|
|
|
|
26
|
|
|
use Guzzle\Http\Exception\ClientErrorResponseException; |
|
27
|
|
|
use OCP\Files\ObjectStore\IObjectStore; |
|
28
|
|
|
use OpenCloud\OpenStack; |
|
29
|
|
|
use OpenCloud\Rackspace; |
|
30
|
|
|
|
|
31
|
|
|
class Swift implements IObjectStore { |
|
32
|
|
|
/** |
|
33
|
|
|
* @var \OpenCloud\OpenStack |
|
34
|
|
|
*/ |
|
35
|
|
|
private $client; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
private $params; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var \OpenCloud\ObjectStore\Service |
|
44
|
|
|
*/ |
|
45
|
|
|
private $objectStoreService; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var \OpenCloud\ObjectStore\Resource\Container |
|
49
|
|
|
*/ |
|
50
|
|
|
private $container; |
|
51
|
|
|
|
|
52
|
|
|
public function __construct($params) { |
|
53
|
|
|
if (!isset($params['container'])) { |
|
54
|
|
|
$params['container'] = 'owncloud'; |
|
55
|
|
|
} |
|
56
|
|
|
if (!isset($params['autocreate'])) { |
|
57
|
|
|
// should only be true for tests |
|
58
|
|
|
$params['autocreate'] = false; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if (isset($params['apiKey'])) { |
|
62
|
|
|
$this->client = new Rackspace($params['url'], $params); |
|
|
|
|
|
|
63
|
|
|
} else { |
|
64
|
|
|
$this->client = new OpenStack($params['url'], $params); |
|
65
|
|
|
} |
|
66
|
|
|
$this->params = $params; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
protected function init() { |
|
70
|
|
|
if ($this->container) { |
|
71
|
|
|
return; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
// the OpenCloud client library will default to 'cloudFiles' if $serviceName is null |
|
75
|
|
|
$serviceName = null; |
|
76
|
|
|
if (isset($this->params['serviceName'])) { |
|
77
|
|
|
$serviceName = $this->params['serviceName']; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
// the OpenCloud client library will default to 'publicURL' if $urlType is null |
|
81
|
|
|
$urlType = null; |
|
82
|
|
|
if (isset($this->params['urlType'])) { |
|
83
|
|
|
$urlType = $this->params['urlType']; |
|
84
|
|
|
} |
|
85
|
|
|
$this->objectStoreService = $this->client->objectStoreService($serviceName, $this->params['region'], $urlType); |
|
86
|
|
|
|
|
87
|
|
|
try { |
|
88
|
|
|
$this->container = $this->objectStoreService->getContainer($this->params['container']); |
|
89
|
|
|
} catch (ClientErrorResponseException $ex) { |
|
|
|
|
|
|
90
|
|
|
// if the container does not exist and autocreate is true try to create the container on the fly |
|
91
|
|
|
if (isset($this->params['autocreate']) && $this->params['autocreate'] === true) { |
|
92
|
|
|
$this->container = $this->objectStoreService->createContainer($this->params['container']); |
|
93
|
|
|
} else { |
|
94
|
|
|
throw $ex; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return string the container name where objects are stored |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getStorageId() { |
|
103
|
|
|
return $this->params['container']; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param string $urn the unified resource name used to identify the object |
|
108
|
|
|
* @param resource $stream stream with the data to write |
|
109
|
|
|
* @throws Exception from openstack lib when something goes wrong |
|
110
|
|
|
*/ |
|
111
|
|
|
public function writeObject($urn, $stream) { |
|
112
|
|
|
$this->init(); |
|
113
|
|
|
$this->container->uploadObject($urn, $stream); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param string $urn the unified resource name used to identify the object |
|
118
|
|
|
* @return resource stream with the read data |
|
119
|
|
|
* @throws Exception from openstack lib when something goes wrong |
|
120
|
|
|
*/ |
|
121
|
|
|
public function readObject($urn) { |
|
122
|
|
|
$this->init(); |
|
123
|
|
|
$object = $this->container->getObject($urn); |
|
124
|
|
|
|
|
125
|
|
|
// we need to keep a reference to objectContent or |
|
126
|
|
|
// the stream will be closed before we can do anything with it |
|
127
|
|
|
/** @var $objectContent \Guzzle\Http\EntityBody * */ |
|
128
|
|
|
$objectContent = $object->getContent(); |
|
129
|
|
|
$objectContent->rewind(); |
|
130
|
|
|
|
|
131
|
|
|
$stream = $objectContent->getStream(); |
|
132
|
|
|
// save the object content in the context of the stream to prevent it being gc'd until the stream is closed |
|
133
|
|
|
stream_context_set_option($stream, 'swift','content', $objectContent); |
|
134
|
|
|
|
|
135
|
|
|
return $stream; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @param string $urn Unified Resource Name |
|
140
|
|
|
* @return void |
|
141
|
|
|
* @throws Exception from openstack lib when something goes wrong |
|
142
|
|
|
*/ |
|
143
|
|
|
public function deleteObject($urn) { |
|
144
|
|
|
$this->init(); |
|
145
|
|
|
// see https://github.com/rackspace/php-opencloud/issues/243#issuecomment-30032242 |
|
146
|
|
|
$this->container->dataObject()->setName($urn)->delete(); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
public function deleteContainer($recursive = false) { |
|
150
|
|
|
$this->init(); |
|
151
|
|
|
$this->container->delete($recursive); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
} |
|
155
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..