@@ -133,8 +133,8 @@ |
||
133 | 133 | } |
134 | 134 | |
135 | 135 | /** |
136 | - * @param $urn |
|
137 | - * @param $stream |
|
136 | + * @param string $urn |
|
137 | + * @param resource $stream |
|
138 | 138 | * @return bool |
139 | 139 | */ |
140 | 140 | function multipartUpload($urn, $stream) { |
@@ -29,156 +29,156 @@ |
||
29 | 29 | |
30 | 30 | class QingStor implements IObjectStore { |
31 | 31 | |
32 | - /** @var array */ |
|
33 | - protected $params; |
|
34 | - |
|
35 | - /** @var string */ |
|
36 | - protected $id; |
|
37 | - |
|
38 | - /** @var QingStor\SDK\Service\Bucket */ |
|
39 | - protected $bucket; |
|
40 | - |
|
41 | - protected function parseParams($params) { |
|
42 | - if (empty($params['key']) || empty($params['secret']) || empty($params['bucket'])) { |
|
43 | - throw new \Exception('Access Key, Secret Key, and Bucket must be configured.'); |
|
44 | - } |
|
45 | - |
|
46 | - $this->id = 'qingstor::' . $params['bucket']; |
|
47 | - |
|
48 | - $params['zone'] = empty($params['zone']) ? 'pek3a' : $params['zone']; |
|
49 | - $params['host'] = empty($params['host']) ? 'qingstor.com' : $params['host']; |
|
50 | - $params['part_size'] = empty($params['part_size']) ? 64 * 1024 * 1024 : $params['part_size'] * 1024 * 1024; |
|
51 | - $params['prefix'] = empty($params['prefix']) ? '' : $params['prefix']; |
|
52 | - if (!isset($params['port']) || $params['port'] === '') { |
|
53 | - $params['port'] = (isset($params['use_ssl']) && $params['use_ssl'] === false) ? 80 : 443; |
|
54 | - } |
|
55 | - $this->params = $params; |
|
56 | - } |
|
57 | - |
|
58 | - protected function getBucket() { |
|
59 | - if (!is_null($this->bucket)) { |
|
60 | - return $this->bucket; |
|
61 | - } |
|
62 | - |
|
63 | - $scheme = (isset($this->params['use_ssl']) && $this->params['use_ssl'] === false) ? 'http' : 'https'; |
|
64 | - |
|
65 | - $config = new QingStorConfig(); |
|
66 | - $config->access_key_id = $this->params['key']; |
|
67 | - $config->secret_access_key = $this->params['secret']; |
|
68 | - $config->host = $this->params['host']; |
|
69 | - $config->port = $this->params['port']; |
|
70 | - $config->protocol = $scheme; |
|
71 | - |
|
72 | - $service = new QingStorService($config); |
|
73 | - $this->bucket = $service->Bucket($this->params['bucket'], $this->params['zone']); |
|
74 | - return $this->bucket; |
|
75 | - } |
|
76 | - |
|
77 | - public function __construct($parameters) { |
|
78 | - $this->parseParams($parameters); |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * @return string the container or bucket name where objects are stored |
|
83 | - * @since 7.0.0 |
|
84 | - */ |
|
85 | - function getStorageId() { |
|
86 | - return $this->id; |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * @param string $urn the unified resource name used to identify the object |
|
91 | - * @return resource stream with the read data |
|
92 | - * @throws \Exception when something goes wrong, message will be logged |
|
93 | - * @since 7.0.0 |
|
94 | - */ |
|
95 | - function readObject($urn) { |
|
96 | - $req = $this->getBucket()->getObjectQuery($urn, time() + 1000); |
|
97 | - |
|
98 | - $headers = $req->getHeaders(); |
|
99 | - $headers[] = 'Connection: close'; |
|
100 | - |
|
101 | - $opts = [ |
|
102 | - 'http' => [ |
|
103 | - 'method' => 'GET', |
|
104 | - 'header' => $headers |
|
105 | - ], |
|
106 | - 'ssl' => [ |
|
107 | - 'verify_peer' => true |
|
108 | - ] |
|
109 | - ]; |
|
110 | - |
|
111 | - $context = stream_context_create($opts); |
|
112 | - return fopen($req->getUrl(), 'r', false, $context); |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * @param string $urn the unified resource name used to identify the object |
|
117 | - * @param resource $stream stream with the data to write |
|
118 | - * @throws \Exception when something goes wrong, message will be logged |
|
119 | - * @since 7.0.0 |
|
120 | - */ |
|
121 | - function writeObject($urn, $stream) { |
|
122 | - $content = fread($stream, $this->params['part_size']); |
|
123 | - if (strlen($content) < $this->params['part_size']) { |
|
124 | - $this->getBucket()->putObject( |
|
125 | - $urn, |
|
126 | - array( |
|
127 | - 'body' => $content |
|
128 | - ) |
|
129 | - ); |
|
130 | - } else { |
|
131 | - $this->multipartUpload($urn, $stream); |
|
132 | - } |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * @param $urn |
|
137 | - * @param $stream |
|
138 | - * @return bool |
|
139 | - */ |
|
140 | - function multipartUpload($urn, $stream) { |
|
141 | - rewind($stream); |
|
142 | - $bucket = $this->getBucket(); |
|
143 | - $upload_id = $bucket->initiateMultipartUpload($urn)->upload_id; |
|
144 | - $parts = array(); |
|
145 | - $part_number = 0; |
|
146 | - $content = fread($stream, $this->params['part_size']); |
|
147 | - while ($content) { |
|
148 | - try { |
|
149 | - $bucket->uploadMultipart($urn, array( |
|
150 | - 'upload_id' => $upload_id, |
|
151 | - 'part_number' => $part_number, |
|
152 | - 'body' => $content |
|
153 | - )); |
|
154 | - } catch (\Exception $ex) { |
|
155 | - \OCP\Util::writeLog('QingStor', $ex->getMessage(), \OCP\Util::ERROR); |
|
156 | - $bucket->abortMultipartUpload($urn, array( |
|
157 | - 'upload_id' => $upload_id |
|
158 | - )); |
|
159 | - return false; |
|
160 | - } |
|
161 | - $parts[] = array( |
|
162 | - 'part_number' => $part_number |
|
163 | - ); |
|
164 | - $part_number += 1; |
|
165 | - $content = fread($stream, $this->params['part_size']); |
|
166 | - } |
|
167 | - $bucket->completeMultipartUpload($urn, array( |
|
168 | - 'upload_id' => $upload_id, |
|
169 | - 'object_parts' => $parts |
|
170 | - )); |
|
171 | - return true; |
|
172 | - } |
|
173 | - |
|
174 | - |
|
175 | - /** |
|
176 | - * @param string $urn the unified resource name used to identify the object |
|
177 | - * @return void |
|
178 | - * @throws \Exception when something goes wrong, message will be logged |
|
179 | - * @since 7.0.0 |
|
180 | - */ |
|
181 | - function deleteObject($urn) { |
|
182 | - $this->getBucket()->deleteObject($urn); |
|
183 | - } |
|
32 | + /** @var array */ |
|
33 | + protected $params; |
|
34 | + |
|
35 | + /** @var string */ |
|
36 | + protected $id; |
|
37 | + |
|
38 | + /** @var QingStor\SDK\Service\Bucket */ |
|
39 | + protected $bucket; |
|
40 | + |
|
41 | + protected function parseParams($params) { |
|
42 | + if (empty($params['key']) || empty($params['secret']) || empty($params['bucket'])) { |
|
43 | + throw new \Exception('Access Key, Secret Key, and Bucket must be configured.'); |
|
44 | + } |
|
45 | + |
|
46 | + $this->id = 'qingstor::' . $params['bucket']; |
|
47 | + |
|
48 | + $params['zone'] = empty($params['zone']) ? 'pek3a' : $params['zone']; |
|
49 | + $params['host'] = empty($params['host']) ? 'qingstor.com' : $params['host']; |
|
50 | + $params['part_size'] = empty($params['part_size']) ? 64 * 1024 * 1024 : $params['part_size'] * 1024 * 1024; |
|
51 | + $params['prefix'] = empty($params['prefix']) ? '' : $params['prefix']; |
|
52 | + if (!isset($params['port']) || $params['port'] === '') { |
|
53 | + $params['port'] = (isset($params['use_ssl']) && $params['use_ssl'] === false) ? 80 : 443; |
|
54 | + } |
|
55 | + $this->params = $params; |
|
56 | + } |
|
57 | + |
|
58 | + protected function getBucket() { |
|
59 | + if (!is_null($this->bucket)) { |
|
60 | + return $this->bucket; |
|
61 | + } |
|
62 | + |
|
63 | + $scheme = (isset($this->params['use_ssl']) && $this->params['use_ssl'] === false) ? 'http' : 'https'; |
|
64 | + |
|
65 | + $config = new QingStorConfig(); |
|
66 | + $config->access_key_id = $this->params['key']; |
|
67 | + $config->secret_access_key = $this->params['secret']; |
|
68 | + $config->host = $this->params['host']; |
|
69 | + $config->port = $this->params['port']; |
|
70 | + $config->protocol = $scheme; |
|
71 | + |
|
72 | + $service = new QingStorService($config); |
|
73 | + $this->bucket = $service->Bucket($this->params['bucket'], $this->params['zone']); |
|
74 | + return $this->bucket; |
|
75 | + } |
|
76 | + |
|
77 | + public function __construct($parameters) { |
|
78 | + $this->parseParams($parameters); |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * @return string the container or bucket name where objects are stored |
|
83 | + * @since 7.0.0 |
|
84 | + */ |
|
85 | + function getStorageId() { |
|
86 | + return $this->id; |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * @param string $urn the unified resource name used to identify the object |
|
91 | + * @return resource stream with the read data |
|
92 | + * @throws \Exception when something goes wrong, message will be logged |
|
93 | + * @since 7.0.0 |
|
94 | + */ |
|
95 | + function readObject($urn) { |
|
96 | + $req = $this->getBucket()->getObjectQuery($urn, time() + 1000); |
|
97 | + |
|
98 | + $headers = $req->getHeaders(); |
|
99 | + $headers[] = 'Connection: close'; |
|
100 | + |
|
101 | + $opts = [ |
|
102 | + 'http' => [ |
|
103 | + 'method' => 'GET', |
|
104 | + 'header' => $headers |
|
105 | + ], |
|
106 | + 'ssl' => [ |
|
107 | + 'verify_peer' => true |
|
108 | + ] |
|
109 | + ]; |
|
110 | + |
|
111 | + $context = stream_context_create($opts); |
|
112 | + return fopen($req->getUrl(), 'r', false, $context); |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * @param string $urn the unified resource name used to identify the object |
|
117 | + * @param resource $stream stream with the data to write |
|
118 | + * @throws \Exception when something goes wrong, message will be logged |
|
119 | + * @since 7.0.0 |
|
120 | + */ |
|
121 | + function writeObject($urn, $stream) { |
|
122 | + $content = fread($stream, $this->params['part_size']); |
|
123 | + if (strlen($content) < $this->params['part_size']) { |
|
124 | + $this->getBucket()->putObject( |
|
125 | + $urn, |
|
126 | + array( |
|
127 | + 'body' => $content |
|
128 | + ) |
|
129 | + ); |
|
130 | + } else { |
|
131 | + $this->multipartUpload($urn, $stream); |
|
132 | + } |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * @param $urn |
|
137 | + * @param $stream |
|
138 | + * @return bool |
|
139 | + */ |
|
140 | + function multipartUpload($urn, $stream) { |
|
141 | + rewind($stream); |
|
142 | + $bucket = $this->getBucket(); |
|
143 | + $upload_id = $bucket->initiateMultipartUpload($urn)->upload_id; |
|
144 | + $parts = array(); |
|
145 | + $part_number = 0; |
|
146 | + $content = fread($stream, $this->params['part_size']); |
|
147 | + while ($content) { |
|
148 | + try { |
|
149 | + $bucket->uploadMultipart($urn, array( |
|
150 | + 'upload_id' => $upload_id, |
|
151 | + 'part_number' => $part_number, |
|
152 | + 'body' => $content |
|
153 | + )); |
|
154 | + } catch (\Exception $ex) { |
|
155 | + \OCP\Util::writeLog('QingStor', $ex->getMessage(), \OCP\Util::ERROR); |
|
156 | + $bucket->abortMultipartUpload($urn, array( |
|
157 | + 'upload_id' => $upload_id |
|
158 | + )); |
|
159 | + return false; |
|
160 | + } |
|
161 | + $parts[] = array( |
|
162 | + 'part_number' => $part_number |
|
163 | + ); |
|
164 | + $part_number += 1; |
|
165 | + $content = fread($stream, $this->params['part_size']); |
|
166 | + } |
|
167 | + $bucket->completeMultipartUpload($urn, array( |
|
168 | + 'upload_id' => $upload_id, |
|
169 | + 'object_parts' => $parts |
|
170 | + )); |
|
171 | + return true; |
|
172 | + } |
|
173 | + |
|
174 | + |
|
175 | + /** |
|
176 | + * @param string $urn the unified resource name used to identify the object |
|
177 | + * @return void |
|
178 | + * @throws \Exception when something goes wrong, message will be logged |
|
179 | + * @since 7.0.0 |
|
180 | + */ |
|
181 | + function deleteObject($urn) { |
|
182 | + $this->getBucket()->deleteObject($urn); |
|
183 | + } |
|
184 | 184 | } |
185 | 185 | \ No newline at end of file |
@@ -43,7 +43,7 @@ |
||
43 | 43 | throw new \Exception('Access Key, Secret Key, and Bucket must be configured.'); |
44 | 44 | } |
45 | 45 | |
46 | - $this->id = 'qingstor::' . $params['bucket']; |
|
46 | + $this->id = 'qingstor::'.$params['bucket']; |
|
47 | 47 | |
48 | 48 | $params['zone'] = empty($params['zone']) ? 'pek3a' : $params['zone']; |
49 | 49 | $params['host'] = empty($params['host']) ? 'qingstor.com' : $params['host']; |