Blame view
sources/apps/files_external/lib/amazons3.php
13.2 KB
|
03e52840d
|
1 2 3 4 5 6 |
<?php /** * ownCloud * * @author Michael Gapczynski |
|
31b7f2792
|
7 |
* @author Christian Berendt |
|
03e52840d
|
8 |
* @copyright 2012 Michael Gapczynski mtgap@owncloud.com |
|
31b7f2792
|
9 |
* @copyright 2013 Christian Berendt berendt@b1-systems.de |
|
03e52840d
|
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
* * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE * License as published by the Free Software Foundation; either * version 3 of the License, or any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU AFFERO GENERAL PUBLIC LICENSE for more details. * * You should have received a copy of the GNU Affero General Public * License along with this library. If not, see <http://www.gnu.org/licenses/>. */ namespace OC\Files\Storage; |
|
31b7f2792
|
26 |
set_include_path(get_include_path() . PATH_SEPARATOR . |
|
6d9380f96
|
27 |
\OC_App::getAppPath('files_external') . '/3rdparty/aws-sdk-php');
|
|
31b7f2792
|
28 29 30 31 |
require 'aws-autoloader.php'; use Aws\S3\S3Client; use Aws\S3\Exception\S3Exception; |
|
03e52840d
|
32 33 |
class AmazonS3 extends \OC\Files\Storage\Common {
|
|
31b7f2792
|
34 35 36 37 38 39 40 |
/** * @var \Aws\S3\S3Client */ private $connection; /** * @var string */ |
|
03e52840d
|
41 |
private $bucket; |
|
31b7f2792
|
42 43 44 45 46 47 48 49 50 51 52 53 |
/** * @var array */ private static $tmpFiles = array(); /** * @var bool */ private $test = false; /** * @var int */ private $timeout = 15; |
|
03e52840d
|
54 |
|
|
6d9380f96
|
55 56 |
/** * @param string $path |
|
f7d878ff1
|
57 |
* @return string correctly encoded path |
|
6d9380f96
|
58 |
*/ |
|
31b7f2792
|
59 60 |
private function normalizePath($path) {
$path = trim($path, '/');
|
|
03e52840d
|
61 |
|
|
31b7f2792
|
62 63 64 |
if (!$path) {
$path = '.';
}
|
|
03e52840d
|
65 |
|
|
31b7f2792
|
66 67 68 69 70 71 |
return $path;
}
private function testTimeout() {
if ($this->test) {
sleep($this->timeout);
|
|
03e52840d
|
72 73 |
} } |
|
6d9380f96
|
74 75 76 77 78 79 |
private function cleanKey($path) {
if ($path === '.') {
return '/';
}
return $path;
}
|
|
03e52840d
|
80 |
|
|
31b7f2792
|
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
public function __construct($params) {
if (!isset($params['key']) || !isset($params['secret']) || !isset($params['bucket'])) {
throw new \Exception("Access Key, Secret and Bucket have to be configured.");
}
$this->id = 'amazon::' . $params['key'] . md5($params['secret']);
$this->bucket = $params['bucket'];
$scheme = ($params['use_ssl'] === 'false') ? 'http' : 'https';
$this->test = isset($params['test']);
$this->timeout = ( ! isset($params['timeout'])) ? 15 : $params['timeout'];
$params['region'] = ( ! isset($params['region']) || $params['region'] === '' ) ? 'eu-west-1' : $params['region'];
$params['hostname'] = ( !isset($params['hostname']) || $params['hostname'] === '' ) ? 's3.amazonaws.com' : $params['hostname'];
if (!isset($params['port']) || $params['port'] === '') {
$params['port'] = ($params['use_ssl'] === 'false') ? 80 : 443;
}
$base_url = $scheme.'://'.$params['hostname'].':'.$params['port'].'/';
$this->connection = S3Client::factory(array(
'key' => $params['key'],
'secret' => $params['secret'],
'base_url' => $base_url,
'region' => $params['region']
));
if (!$this->connection->isValidBucketName($this->bucket)) {
throw new \Exception("The configured bucket name is invalid.");
}
if (!$this->connection->doesBucketExist($this->bucket)) {
try {
$result = $this->connection->createBucket(array(
'Bucket' => $this->bucket
));
$this->connection->waitUntilBucketExists(array(
'Bucket' => $this->bucket,
'waiter.interval' => 1,
'waiter.max_attempts' => 15
));
$this->testTimeout();
} catch (S3Exception $e) {
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
throw new \Exception("Creation of bucket failed.");
|
|
03e52840d
|
124 125 |
} } |
|
31b7f2792
|
126 127 128 |
if (!$this->file_exists('.')) {
$result = $this->connection->putObject(array(
'Bucket' => $this->bucket,
|
|
6d9380f96
|
129 |
'Key' => $this->cleanKey('.'),
|
|
31b7f2792
|
130 131 132 133 134 135 |
'Body' => '', 'ContentType' => 'httpd/unix-directory', 'ContentLength' => 0 )); $this->testTimeout(); } |
|
03e52840d
|
136 137 138 |
}
public function mkdir($path) {
|
|
31b7f2792
|
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
$path = $this->normalizePath($path);
if ($this->is_dir($path)) {
return false;
}
try {
$result = $this->connection->putObject(array(
'Bucket' => $this->bucket,
'Key' => $path . '/',
'Body' => '',
'ContentType' => 'httpd/unix-directory',
'ContentLength' => 0
));
$this->testTimeout();
} catch (S3Exception $e) {
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
return false;
}
return true;
}
public function file_exists($path) {
$path = $this->normalizePath($path);
if (!$path) {
$path = '.';
} else if ($path != '.' && $this->is_dir($path)) {
|
|
03e52840d
|
168 169 |
$path .= '/'; } |
|
31b7f2792
|
170 171 172 173 |
try {
$result = $this->connection->doesObjectExist(
$this->bucket,
|
|
6d9380f96
|
174 |
$this->cleanKey($path) |
|
31b7f2792
|
175 176 177 178 179 180 181 |
);
} catch (S3Exception $e) {
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
return false;
}
return $result;
|
|
03e52840d
|
182 |
} |
|
31b7f2792
|
183 |
|
|
03e52840d
|
184 |
public function rmdir($path) {
|
|
31b7f2792
|
185 186 187 188 |
$path = $this->normalizePath($path);
if (!$this->file_exists($path)) {
return false;
|
|
03e52840d
|
189 |
} |
|
31b7f2792
|
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
$dh = $this->opendir($path);
if(is_resource($dh)) {
while (($file = readdir($dh)) !== false) {
if ($file === '.' || $file === '..') {
continue;
}
if ($this->is_dir($path . '/' . $file)) {
$this->rmdir($path . '/' . $file);
} else {
$this->unlink($path . '/' . $file);
}
}
}
try {
$result = $this->connection->deleteObject(array(
'Bucket' => $this->bucket,
'Key' => $path . '/'
));
$this->testTimeout();
} catch (S3Exception $e) {
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
return false;
}
return true;
|
|
03e52840d
|
219 220 221 |
}
public function opendir($path) {
|
|
31b7f2792
|
222 223 224 225 226 227 |
$path = $this->normalizePath($path);
if ($path === '.') {
$path = '';
} else if ($path) {
$path .= '/';
|
|
03e52840d
|
228 |
} |
|
31b7f2792
|
229 230 |
try {
|
|
03e52840d
|
231 |
$files = array(); |
|
31b7f2792
|
232 233 234 235 236 237 238 239 240 241 242 243 244 |
$result = $this->connection->getIterator('ListObjects', array(
'Bucket' => $this->bucket,
'Delimiter' => '/',
'Prefix' => $path
), array('return_prefixes' => true));
foreach ($result as $object) {
$file = basename(
isset($object['Key']) ? $object['Key'] : $object['Prefix']
);
if ($file != basename($path)) {
$files[] = $file;
|
|
03e52840d
|
245 246 |
} } |
|
31b7f2792
|
247 |
|
|
03e52840d
|
248 |
\OC\Files\Stream\Dir::register('amazons3' . $path, $files);
|
|
31b7f2792
|
249 |
|
|
03e52840d
|
250 |
return opendir('fakedir://amazons3' . $path);
|
|
31b7f2792
|
251 252 253 |
} catch (S3Exception $e) {
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
return false;
|
|
03e52840d
|
254 |
} |
|
03e52840d
|
255 256 257 |
}
public function stat($path) {
|
|
31b7f2792
|
258 259 260 261 262 263 264 265 266 |
$path = $this->normalizePath($path);
try {
if ($this->is_dir($path) && $path != '.') {
$path .= '/';
}
$result = $this->connection->headObject(array(
'Bucket' => $this->bucket,
|
|
6d9380f96
|
267 |
'Key' => $this->cleanKey($path) |
|
31b7f2792
|
268 269 270 271 272 273 274 275 276 |
));
$stat = array();
$stat['size'] = $result['ContentLength'] ? $result['ContentLength'] : 0;
if ($result['Metadata']['lastmodified']) {
$stat['mtime'] = strtotime($result['Metadata']['lastmodified']);
} else {
$stat['mtime'] = strtotime($result['LastModified']);
}
|
|
03e52840d
|
277 |
$stat['atime'] = time(); |
|
31b7f2792
|
278 |
|
|
03e52840d
|
279 |
return $stat; |
|
31b7f2792
|
280 281 282 |
} catch(S3Exception $e) {
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
return false;
|
|
03e52840d
|
283 |
} |
|
03e52840d
|
284 285 286 |
}
public function filetype($path) {
|
|
31b7f2792
|
287 288 289 290 291 292 293 294 295 |
$path = $this->normalizePath($path);
try {
if ($path != '.' && $this->connection->doesObjectExist($this->bucket, $path)) {
return 'file';
}
if ($path != '.') {
$path .= '/';
|
|
03e52840d
|
296 |
} |
|
6d9380f96
|
297 |
if ($this->connection->doesObjectExist($this->bucket, $this->cleanKey($path))) {
|
|
31b7f2792
|
298 299 300 301 302 |
return 'dir';
}
} catch (S3Exception $e) {
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
return false;
|
|
03e52840d
|
303 |
} |
|
03e52840d
|
304 |
|
|
31b7f2792
|
305 |
return false; |
|
03e52840d
|
306 |
} |
|
31b7f2792
|
307 308 |
public function unlink($path) {
$path = $this->normalizePath($path);
|
|
03e52840d
|
309 |
|
|
31b7f2792
|
310 311 312 |
try {
$result = $this->connection->deleteObject(array(
'Bucket' => $this->bucket,
|
|
6d9380f96
|
313 |
'Key' => $this->cleanKey($path) |
|
31b7f2792
|
314 315 316 317 318 |
));
$this->testTimeout();
} catch (S3Exception $e) {
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
return false;
|
|
03e52840d
|
319 |
} |
|
03e52840d
|
320 |
|
|
31b7f2792
|
321 |
return true; |
|
03e52840d
|
322 323 324 |
}
public function fopen($path, $mode) {
|
|
31b7f2792
|
325 |
$path = $this->normalizePath($path); |
|
03e52840d
|
326 327 328 329 |
switch ($mode) {
case 'r':
case 'rb':
$tmpFile = \OC_Helper::tmpFile();
|
|
31b7f2792
|
330 331 332 333 334 |
self::$tmpFiles[$tmpFile] = $path;
try {
$result = $this->connection->getObject(array(
'Bucket' => $this->bucket,
|
|
6d9380f96
|
335 |
'Key' => $this->cleanKey($path), |
|
31b7f2792
|
336 337 338 339 340 |
'SaveAs' => $tmpFile
));
} catch (S3Exception $e) {
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
return false;
|
|
03e52840d
|
341 |
} |
|
31b7f2792
|
342 343 |
return fopen($tmpFile, 'r'); |
|
03e52840d
|
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
case 'w':
case 'wb':
case 'a':
case 'ab':
case 'r+':
case 'w+':
case 'wb+':
case 'a+':
case 'x':
case 'x+':
case 'c':
case 'c+':
if (strrpos($path, '.') !== false) {
$ext = substr($path, strrpos($path, '.'));
} else {
$ext = '';
}
$tmpFile = \OC_Helper::tmpFile($ext);
\OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack'));
if ($this->file_exists($path)) {
$source = $this->fopen($path, 'r');
file_put_contents($tmpFile, $source);
}
|
|
31b7f2792
|
367 |
self::$tmpFiles[$tmpFile] = $path; |
|
03e52840d
|
368 369 370 371 |
return fopen('close://' . $tmpFile, $mode);
}
return false;
}
|
|
03e52840d
|
372 |
public function getMimeType($path) {
|
|
31b7f2792
|
373 374 375 |
$path = $this->normalizePath($path);
if ($this->is_dir($path)) {
|
|
03e52840d
|
376 |
return 'httpd/unix-directory'; |
|
31b7f2792
|
377 378 379 380 |
} else if ($this->file_exists($path)) {
try {
$result = $this->connection->headObject(array(
'Bucket' => $this->bucket,
|
|
6d9380f96
|
381 |
'Key' => $this->cleanKey($path) |
|
31b7f2792
|
382 383 384 385 |
));
} catch (S3Exception $e) {
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
return false;
|
|
03e52840d
|
386 |
} |
|
31b7f2792
|
387 388 |
return $result['ContentType']; |
|
03e52840d
|
389 390 391 392 393 |
}
return false;
}
public function touch($path, $mtime = null) {
|
|
31b7f2792
|
394 395 396 397 398 |
$path = $this->normalizePath($path);
$metadata = array();
if (!is_null($mtime)) {
$metadata = array('lastmodified' => $mtime);
|
|
03e52840d
|
399 |
} |
|
31b7f2792
|
400 401 402 403 404 405 406 407 |
try {
if ($this->file_exists($path)) {
if ($this->is_dir($path) && $path != '.') {
$path .= '/';
}
$result = $this->connection->copyObject(array(
'Bucket' => $this->bucket,
|
|
6d9380f96
|
408 |
'Key' => $this->cleanKey($path), |
|
31b7f2792
|
409 410 411 412 413 414 415 |
'Metadata' => $metadata,
'CopySource' => $this->bucket . '/' . $path
));
$this->testTimeout();
} else {
$result = $this->connection->putObject(array(
'Bucket' => $this->bucket,
|
|
6d9380f96
|
416 |
'Key' => $this->cleanKey($path), |
|
31b7f2792
|
417 418 419 420 421 422 423 |
'Metadata' => $metadata
));
$this->testTimeout();
}
} catch (S3Exception $e) {
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
return false;
|
|
03e52840d
|
424 |
} |
|
31b7f2792
|
425 426 427 428 429 430 431 432 433 434 435 436 |
return true;
}
public function copy($path1, $path2) {
$path1 = $this->normalizePath($path1);
$path2 = $this->normalizePath($path2);
if ($this->is_file($path1)) {
try {
$result = $this->connection->copyObject(array(
'Bucket' => $this->bucket,
|
|
6d9380f96
|
437 |
'Key' => $this->cleanKey($path2), |
|
f7d878ff1
|
438 |
'CopySource' => S3Client::encodeKey($this->bucket . '/' . $path1) |
|
31b7f2792
|
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 |
));
$this->testTimeout();
} catch (S3Exception $e) {
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
return false;
}
} else {
if ($this->file_exists($path2)) {
return false;
}
try {
$result = $this->connection->copyObject(array(
'Bucket' => $this->bucket,
'Key' => $path2 . '/',
|
|
f7d878ff1
|
454 |
'CopySource' => S3Client::encodeKey($this->bucket . '/' . $path1 . '/') |
|
31b7f2792
|
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 |
));
$this->testTimeout();
} catch (S3Exception $e) {
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
return false;
}
$dh = $this->opendir($path1);
if(is_resource($dh)) {
while (($file = readdir($dh)) !== false) {
if ($file === '.' || $file === '..') {
continue;
}
$source = $path1 . '/' . $file;
$target = $path2 . '/' . $file;
$this->copy($source, $target);
}
}
}
return true;
}
public function rename($path1, $path2) {
$path1 = $this->normalizePath($path1);
$path2 = $this->normalizePath($path2);
if ($this->is_file($path1)) {
if ($this->copy($path1, $path2) === false) {
return false;
}
if ($this->unlink($path1) === false) {
$this->unlink($path2);
return false;
}
} else {
if ($this->file_exists($path2)) {
return false;
}
if ($this->copy($path1, $path2) === false) {
return false;
}
if ($this->rmdir($path1) === false) {
$this->rmdir($path2);
return false;
}
}
return true;
|
|
03e52840d
|
508 509 510 |
}
public function test() {
|
|
31b7f2792
|
511 512 513 514 |
$test = $this->connection->getBucketAcl(array(
'Bucket' => $this->bucket,
));
if (isset($test) && !is_null($test->getPath('Owner/ID'))) {
|
|
03e52840d
|
515 516 517 518 |
return true; } return false; } |
|
31b7f2792
|
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 |
public function getId() {
return $this->id;
}
public function getConnection() {
return $this->connection;
}
public function writeBack($tmpFile) {
if (!isset(self::$tmpFiles[$tmpFile])) {
return false;
}
try {
$result= $this->connection->putObject(array(
'Bucket' => $this->bucket,
|
|
6d9380f96
|
535 |
'Key' => $this->cleanKey(self::$tmpFiles[$tmpFile]), |
|
31b7f2792
|
536 537 538 539 540 541 542 543 544 545 546 547 |
'SourceFile' => $tmpFile,
'ContentType' => \OC_Helper::getMimeType($tmpFile),
'ContentLength' => filesize($tmpFile)
));
$this->testTimeout();
unlink($tmpFile);
} catch (S3Exception $e) {
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
return false;
}
}
|
|
6d9380f96
|
548 549 550 551 552 553 554 555 556 557 558 |
/**
* check if curl is installed
*/
public static function checkDependencies() {
if (function_exists('curl_init')) {
return true;
} else {
return array('curl');
}
}
|
|
03e52840d
|
559 |
} |