AmazonS3.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author André Gaul <gaul@web-yard.de>
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Christian Berendt <berendt@b1-systems.de>
  8. * @author Christopher T. Johnson <ctjctj@gmail.com>
  9. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  10. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  11. * @author enoch <lanxenet@hotmail.com>
  12. * @author Johan Björk <johanimon@gmail.com>
  13. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  14. * @author Julius Härtl <jus@bitgrid.net>
  15. * @author Martin Mattel <martin.mattel@diemattels.at>
  16. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  17. * @author Morris Jobke <hey@morrisjobke.de>
  18. * @author Philipp Kapfer <philipp.kapfer@gmx.at>
  19. * @author Robin Appelman <robin@icewind.nl>
  20. * @author Robin McCorkell <robin@mccorkell.me.uk>
  21. * @author Roeland Jago Douma <roeland@famdouma.nl>
  22. * @author Thomas Müller <thomas.mueller@tmit.eu>
  23. * @author Vincent Petry <vincent@nextcloud.com>
  24. *
  25. * @license AGPL-3.0
  26. *
  27. * This code is free software: you can redistribute it and/or modify
  28. * it under the terms of the GNU Affero General Public License, version 3,
  29. * as published by the Free Software Foundation.
  30. *
  31. * This program is distributed in the hope that it will be useful,
  32. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  34. * GNU Affero General Public License for more details.
  35. *
  36. * You should have received a copy of the GNU Affero General Public License, version 3,
  37. * along with this program. If not, see <http://www.gnu.org/licenses/>
  38. *
  39. */
  40. namespace OCA\Files_External\Lib\Storage;
  41. use Aws\Result;
  42. use Aws\S3\Exception\S3Exception;
  43. use Aws\S3\S3Client;
  44. use Icewind\Streams\CallbackWrapper;
  45. use Icewind\Streams\IteratorDirectory;
  46. use OC\Cache\CappedMemoryCache;
  47. use OC\Files\Cache\CacheEntry;
  48. use OC\Files\ObjectStore\S3ConnectionTrait;
  49. use OC\Files\ObjectStore\S3ObjectTrait;
  50. use OCP\Constants;
  51. use OCP\Files\FileInfo;
  52. use OCP\Files\IMimeTypeDetector;
  53. use OCP\ICacheFactory;
  54. use OCP\IMemcache;
  55. class AmazonS3 extends \OC\Files\Storage\Common {
  56. use S3ConnectionTrait;
  57. use S3ObjectTrait;
  58. public function needsPartFile() {
  59. return false;
  60. }
  61. /** @var CappedMemoryCache|Result[] */
  62. private $objectCache;
  63. /** @var CappedMemoryCache|bool[] */
  64. private $directoryCache;
  65. /** @var CappedMemoryCache|array */
  66. private $filesCache;
  67. /** @var IMimeTypeDetector */
  68. private $mimeDetector;
  69. /** @var bool|null */
  70. private $versioningEnabled = null;
  71. /** @var IMemcache */
  72. private $memCache;
  73. public function __construct($parameters) {
  74. parent::__construct($parameters);
  75. $this->parseParams($parameters);
  76. $this->id = 'amazon::external::' . md5($this->params['hostname'] . ':' . $this->params['bucket'] . ':' . $this->params['key']);
  77. $this->objectCache = new CappedMemoryCache();
  78. $this->directoryCache = new CappedMemoryCache();
  79. $this->filesCache = new CappedMemoryCache();
  80. $this->mimeDetector = \OC::$server->get(IMimeTypeDetector::class);
  81. /** @var ICacheFactory $cacheFactory */
  82. $cacheFactory = \OC::$server->get(ICacheFactory::class);
  83. $this->memCache = $cacheFactory->createLocal('s3-external');
  84. }
  85. /**
  86. * @param string $path
  87. * @return string correctly encoded path
  88. */
  89. private function normalizePath($path) {
  90. $path = trim($path, '/');
  91. if (!$path) {
  92. $path = '.';
  93. }
  94. return $path;
  95. }
  96. private function isRoot($path) {
  97. return $path === '.';
  98. }
  99. private function cleanKey($path) {
  100. if ($this->isRoot($path)) {
  101. return '/';
  102. }
  103. return $path;
  104. }
  105. private function clearCache() {
  106. $this->objectCache = new CappedMemoryCache();
  107. $this->directoryCache = new CappedMemoryCache();
  108. $this->filesCache = new CappedMemoryCache();
  109. }
  110. private function invalidateCache($key) {
  111. unset($this->objectCache[$key]);
  112. $keys = array_keys($this->objectCache->getData());
  113. $keyLength = strlen($key);
  114. foreach ($keys as $existingKey) {
  115. if (substr($existingKey, 0, $keyLength) === $key) {
  116. unset($this->objectCache[$existingKey]);
  117. }
  118. }
  119. unset($this->filesCache[$key]);
  120. $keys = array_keys($this->directoryCache->getData());
  121. $keyLength = strlen($key);
  122. foreach ($keys as $existingKey) {
  123. if (substr($existingKey, 0, $keyLength) === $key) {
  124. unset($this->directoryCache[$existingKey]);
  125. }
  126. }
  127. unset($this->directoryCache[$key]);
  128. }
  129. /**
  130. * @param $key
  131. * @return array|false
  132. */
  133. private function headObject($key) {
  134. if (!isset($this->objectCache[$key])) {
  135. try {
  136. $this->objectCache[$key] = $this->getConnection()->headObject([
  137. 'Bucket' => $this->bucket,
  138. 'Key' => $key
  139. ])->toArray();
  140. } catch (S3Exception $e) {
  141. if ($e->getStatusCode() >= 500) {
  142. throw $e;
  143. }
  144. $this->objectCache[$key] = false;
  145. }
  146. }
  147. if (is_array($this->objectCache[$key]) && !isset($this->objectCache[$key]["Key"])) {
  148. $this->objectCache[$key]["Key"] = $key;
  149. }
  150. return $this->objectCache[$key];
  151. }
  152. /**
  153. * Return true if directory exists
  154. *
  155. * There are no folders in s3. A folder like structure could be archived
  156. * by prefixing files with the folder name.
  157. *
  158. * Implementation from flysystem-aws-s3-v3:
  159. * https://github.com/thephpleague/flysystem-aws-s3-v3/blob/8241e9cc5b28f981e0d24cdaf9867f14c7498ae4/src/AwsS3Adapter.php#L670-L694
  160. *
  161. * @param $path
  162. * @return bool
  163. * @throws \Exception
  164. */
  165. private function doesDirectoryExist($path) {
  166. if ($path === '.' || $path === '') {
  167. return true;
  168. }
  169. $path = rtrim($path, '/') . '/';
  170. if (isset($this->directoryCache[$path])) {
  171. return $this->directoryCache[$path];
  172. }
  173. try {
  174. // Maybe this isn't an actual key, but a prefix.
  175. // Do a prefix listing of objects to determine.
  176. $result = $this->getConnection()->listObjectsV2([
  177. 'Bucket' => $this->bucket,
  178. 'Prefix' => $path,
  179. 'MaxKeys' => 1,
  180. ]);
  181. if (isset($result['Contents'])) {
  182. $this->directoryCache[$path] = true;
  183. return true;
  184. }
  185. // empty directories have their own object
  186. $object = $this->headObject($path);
  187. if ($object) {
  188. $this->directoryCache[$path] = true;
  189. return true;
  190. }
  191. } catch (S3Exception $e) {
  192. if ($e->getStatusCode() >= 400 && $e->getStatusCode() < 500) {
  193. $this->directoryCache[$path] = false;
  194. }
  195. throw $e;
  196. }
  197. $this->directoryCache[$path] = false;
  198. return false;
  199. }
  200. /**
  201. * Remove a file or folder
  202. *
  203. * @param string $path
  204. * @return bool
  205. */
  206. protected function remove($path) {
  207. // remember fileType to reduce http calls
  208. $fileType = $this->filetype($path);
  209. if ($fileType === 'dir') {
  210. return $this->rmdir($path);
  211. } elseif ($fileType === 'file') {
  212. return $this->unlink($path);
  213. } else {
  214. return false;
  215. }
  216. }
  217. public function mkdir($path) {
  218. $path = $this->normalizePath($path);
  219. if ($this->is_dir($path)) {
  220. return false;
  221. }
  222. try {
  223. $this->getConnection()->putObject([
  224. 'Bucket' => $this->bucket,
  225. 'Key' => $path . '/',
  226. 'Body' => '',
  227. 'ContentType' => FileInfo::MIMETYPE_FOLDER
  228. ]);
  229. $this->testTimeout();
  230. } catch (S3Exception $e) {
  231. \OC::$server->getLogger()->logException($e, ['app' => 'files_external']);
  232. return false;
  233. }
  234. $this->invalidateCache($path);
  235. return true;
  236. }
  237. public function file_exists($path) {
  238. return $this->filetype($path) !== false;
  239. }
  240. public function rmdir($path) {
  241. $path = $this->normalizePath($path);
  242. if ($this->isRoot($path)) {
  243. return $this->clearBucket();
  244. }
  245. if (!$this->file_exists($path)) {
  246. return false;
  247. }
  248. $this->invalidateCache($path);
  249. return $this->batchDelete($path);
  250. }
  251. protected function clearBucket() {
  252. $this->clearCache();
  253. try {
  254. $this->getConnection()->clearBucket([
  255. "Bucket" => $this->bucket
  256. ]);
  257. return true;
  258. // clearBucket() is not working with Ceph, so if it fails we try the slower approach
  259. } catch (\Exception $e) {
  260. return $this->batchDelete();
  261. }
  262. }
  263. private function batchDelete($path = null) {
  264. $params = [
  265. 'Bucket' => $this->bucket
  266. ];
  267. if ($path !== null) {
  268. $params['Prefix'] = $path . '/';
  269. }
  270. try {
  271. $connection = $this->getConnection();
  272. // Since there are no real directories on S3, we need
  273. // to delete all objects prefixed with the path.
  274. do {
  275. // instead of the iterator, manually loop over the list ...
  276. $objects = $connection->listObjects($params);
  277. // ... so we can delete the files in batches
  278. if (isset($objects['Contents'])) {
  279. $connection->deleteObjects([
  280. 'Bucket' => $this->bucket,
  281. 'Delete' => [
  282. 'Objects' => $objects['Contents']
  283. ]
  284. ]);
  285. $this->testTimeout();
  286. }
  287. // we reached the end when the list is no longer truncated
  288. } while ($objects['IsTruncated']);
  289. if ($path !== '' && $path !== null) {
  290. $this->deleteObject($path);
  291. }
  292. } catch (S3Exception $e) {
  293. \OC::$server->getLogger()->logException($e, ['app' => 'files_external']);
  294. return false;
  295. }
  296. return true;
  297. }
  298. public function opendir($path) {
  299. try {
  300. $content = iterator_to_array($this->getDirectoryContent($path));
  301. return IteratorDirectory::wrap(array_map(function (array $item) {
  302. return $item['name'];
  303. }, $content));
  304. } catch (S3Exception $e) {
  305. return false;
  306. }
  307. }
  308. public function stat($path) {
  309. $path = $this->normalizePath($path);
  310. if ($this->is_dir($path)) {
  311. $stat = $this->getDirectoryMetaData($path);
  312. } else {
  313. $object = $this->headObject($path);
  314. if ($object === false) {
  315. return false;
  316. }
  317. $stat = $this->objectToMetaData($object);
  318. }
  319. $stat['atime'] = time();
  320. return $stat;
  321. }
  322. /**
  323. * Return content length for object
  324. *
  325. * When the information is already present (e.g. opendir has been called before)
  326. * this value is return. Otherwise a headObject is emitted.
  327. *
  328. * @param $path
  329. * @return int|mixed
  330. */
  331. private function getContentLength($path) {
  332. if (isset($this->filesCache[$path])) {
  333. return (int)$this->filesCache[$path]['ContentLength'];
  334. }
  335. $result = $this->headObject($path);
  336. if (isset($result['ContentLength'])) {
  337. return (int)$result['ContentLength'];
  338. }
  339. return 0;
  340. }
  341. /**
  342. * Return last modified for object
  343. *
  344. * When the information is already present (e.g. opendir has been called before)
  345. * this value is return. Otherwise a headObject is emitted.
  346. *
  347. * @param $path
  348. * @return mixed|string
  349. */
  350. private function getLastModified($path) {
  351. if (isset($this->filesCache[$path])) {
  352. return $this->filesCache[$path]['LastModified'];
  353. }
  354. $result = $this->headObject($path);
  355. if (isset($result['LastModified'])) {
  356. return $result['LastModified'];
  357. }
  358. return 'now';
  359. }
  360. public function is_dir($path) {
  361. $path = $this->normalizePath($path);
  362. if (isset($this->filesCache[$path])) {
  363. return false;
  364. }
  365. try {
  366. return $this->doesDirectoryExist($path);
  367. } catch (S3Exception $e) {
  368. \OC::$server->getLogger()->logException($e, ['app' => 'files_external']);
  369. return false;
  370. }
  371. }
  372. public function filetype($path) {
  373. $path = $this->normalizePath($path);
  374. if ($this->isRoot($path)) {
  375. return 'dir';
  376. }
  377. try {
  378. if (isset($this->directoryCache[$path]) && $this->directoryCache[$path]) {
  379. return 'dir';
  380. }
  381. if (isset($this->filesCache[$path]) || $this->headObject($path)) {
  382. return 'file';
  383. }
  384. if ($this->doesDirectoryExist($path)) {
  385. return 'dir';
  386. }
  387. } catch (S3Exception $e) {
  388. \OC::$server->getLogger()->logException($e, ['app' => 'files_external']);
  389. return false;
  390. }
  391. return false;
  392. }
  393. public function getPermissions($path) {
  394. $type = $this->filetype($path);
  395. if (!$type) {
  396. return 0;
  397. }
  398. return $type === 'dir' ? Constants::PERMISSION_ALL : Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE;
  399. }
  400. public function unlink($path) {
  401. $path = $this->normalizePath($path);
  402. if ($this->is_dir($path)) {
  403. return $this->rmdir($path);
  404. }
  405. try {
  406. $this->deleteObject($path);
  407. $this->invalidateCache($path);
  408. } catch (S3Exception $e) {
  409. \OC::$server->getLogger()->logException($e, ['app' => 'files_external']);
  410. return false;
  411. }
  412. return true;
  413. }
  414. public function fopen($path, $mode) {
  415. $path = $this->normalizePath($path);
  416. switch ($mode) {
  417. case 'r':
  418. case 'rb':
  419. // Don't try to fetch empty files
  420. $stat = $this->stat($path);
  421. if (is_array($stat) && isset($stat['size']) && $stat['size'] === 0) {
  422. return fopen('php://memory', $mode);
  423. }
  424. try {
  425. return $this->readObject($path);
  426. } catch (S3Exception $e) {
  427. \OC::$server->getLogger()->logException($e, ['app' => 'files_external']);
  428. return false;
  429. }
  430. case 'w':
  431. case 'wb':
  432. $tmpFile = \OC::$server->getTempManager()->getTemporaryFile();
  433. $handle = fopen($tmpFile, 'w');
  434. return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) {
  435. $this->writeBack($tmpFile, $path);
  436. });
  437. case 'a':
  438. case 'ab':
  439. case 'r+':
  440. case 'w+':
  441. case 'wb+':
  442. case 'a+':
  443. case 'x':
  444. case 'x+':
  445. case 'c':
  446. case 'c+':
  447. if (strrpos($path, '.') !== false) {
  448. $ext = substr($path, strrpos($path, '.'));
  449. } else {
  450. $ext = '';
  451. }
  452. $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext);
  453. if ($this->file_exists($path)) {
  454. $source = $this->readObject($path);
  455. file_put_contents($tmpFile, $source);
  456. }
  457. $handle = fopen($tmpFile, $mode);
  458. return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) {
  459. $this->writeBack($tmpFile, $path);
  460. });
  461. }
  462. return false;
  463. }
  464. public function touch($path, $mtime = null) {
  465. if (is_null($mtime)) {
  466. $mtime = time();
  467. }
  468. $metadata = [
  469. 'lastmodified' => gmdate(\DateTime::RFC1123, $mtime)
  470. ];
  471. try {
  472. if (!$this->file_exists($path)) {
  473. $mimeType = $this->mimeDetector->detectPath($path);
  474. $this->getConnection()->putObject([
  475. 'Bucket' => $this->bucket,
  476. 'Key' => $this->cleanKey($path),
  477. 'Metadata' => $metadata,
  478. 'Body' => '',
  479. 'ContentType' => $mimeType,
  480. 'MetadataDirective' => 'REPLACE',
  481. ]);
  482. $this->testTimeout();
  483. }
  484. } catch (S3Exception $e) {
  485. \OC::$server->getLogger()->logException($e, ['app' => 'files_external']);
  486. return false;
  487. }
  488. $this->invalidateCache($path);
  489. return true;
  490. }
  491. public function copy($path1, $path2, $isFile = null) {
  492. $path1 = $this->normalizePath($path1);
  493. $path2 = $this->normalizePath($path2);
  494. if ($isFile === true || $this->is_file($path1)) {
  495. try {
  496. $this->getConnection()->copyObject([
  497. 'Bucket' => $this->bucket,
  498. 'Key' => $this->cleanKey($path2),
  499. 'CopySource' => S3Client::encodeKey($this->bucket . '/' . $path1)
  500. ]);
  501. $this->testTimeout();
  502. } catch (S3Exception $e) {
  503. \OC::$server->getLogger()->logException($e, ['app' => 'files_external']);
  504. return false;
  505. }
  506. } else {
  507. $this->remove($path2);
  508. try {
  509. $this->mkdir($path2);
  510. $this->testTimeout();
  511. } catch (S3Exception $e) {
  512. \OC::$server->getLogger()->logException($e, ['app' => 'files_external']);
  513. return false;
  514. }
  515. foreach ($this->getDirectoryContent($path1) as $item) {
  516. $source = $path1 . '/' . $item['name'];
  517. $target = $path2 . '/' . $item['name'];
  518. $this->copy($source, $target, $item['mimetype'] !== FileInfo::MIMETYPE_FOLDER);
  519. }
  520. }
  521. $this->invalidateCache($path2);
  522. return true;
  523. }
  524. public function rename($path1, $path2) {
  525. $path1 = $this->normalizePath($path1);
  526. $path2 = $this->normalizePath($path2);
  527. if ($this->is_file($path1)) {
  528. if ($this->copy($path1, $path2) === false) {
  529. return false;
  530. }
  531. if ($this->unlink($path1) === false) {
  532. $this->unlink($path2);
  533. return false;
  534. }
  535. } else {
  536. if ($this->copy($path1, $path2) === false) {
  537. return false;
  538. }
  539. if ($this->rmdir($path1) === false) {
  540. $this->rmdir($path2);
  541. return false;
  542. }
  543. }
  544. return true;
  545. }
  546. public function test() {
  547. $this->getConnection()->headBucket([
  548. 'Bucket' => $this->bucket
  549. ]);
  550. return true;
  551. }
  552. public function getId() {
  553. return $this->id;
  554. }
  555. public function writeBack($tmpFile, $path) {
  556. try {
  557. $source = fopen($tmpFile, 'r');
  558. $this->writeObject($path, $source, $this->mimeDetector->detectPath($path));
  559. $this->invalidateCache($path);
  560. unlink($tmpFile);
  561. return true;
  562. } catch (S3Exception $e) {
  563. \OC::$server->getLogger()->logException($e, ['app' => 'files_external']);
  564. return false;
  565. }
  566. }
  567. /**
  568. * check if curl is installed
  569. */
  570. public static function checkDependencies() {
  571. return true;
  572. }
  573. public function getDirectoryContent($directory): \Traversable {
  574. $path = $this->normalizePath($directory);
  575. if ($this->isRoot($path)) {
  576. $path = '';
  577. } else {
  578. $path .= '/';
  579. }
  580. $results = $this->getConnection()->getPaginator('ListObjectsV2', [
  581. 'Bucket' => $this->bucket,
  582. 'Delimiter' => '/',
  583. 'Prefix' => $path,
  584. ]);
  585. foreach ($results as $result) {
  586. // sub folders
  587. if (is_array($result['CommonPrefixes'])) {
  588. foreach ($result['CommonPrefixes'] as $prefix) {
  589. $dir = $this->getDirectoryMetaData($prefix['Prefix']);
  590. if ($dir) {
  591. yield $dir;
  592. }
  593. }
  594. }
  595. if (is_array($result['Contents'])) {
  596. foreach ($result['Contents'] as $object) {
  597. $this->objectCache[$object['Key']] = $object;
  598. if ($object['Key'] !== $path) {
  599. yield $this->objectToMetaData($object);
  600. }
  601. }
  602. }
  603. }
  604. }
  605. private function objectToMetaData(array $object): array {
  606. return [
  607. 'name' => basename($object['Key']),
  608. 'mimetype' => $this->mimeDetector->detectPath($object['Key']),
  609. 'mtime' => strtotime($object['LastModified']),
  610. 'storage_mtime' => strtotime($object['LastModified']),
  611. 'etag' => $object['ETag'],
  612. 'permissions' => Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE,
  613. 'size' => (int)($object['Size'] ?? $object['ContentLength']),
  614. ];
  615. }
  616. private function getDirectoryMetaData(string $path): ?array {
  617. $path = trim($path, '/');
  618. // when versioning is enabled, delete markers are returned as part of CommonPrefixes
  619. // resulting in "ghost" folders, verify that each folder actually exists
  620. if ($this->versioningEnabled() && !$this->doesDirectoryExist($path)) {
  621. return null;
  622. }
  623. $cacheEntry = $this->getCache()->get($path);
  624. if ($cacheEntry instanceof CacheEntry) {
  625. return $cacheEntry->getData();
  626. } else {
  627. return [
  628. 'name' => basename($path),
  629. 'mimetype' => FileInfo::MIMETYPE_FOLDER,
  630. 'mtime' => time(),
  631. 'storage_mtime' => time(),
  632. 'etag' => uniqid(),
  633. 'permissions' => Constants::PERMISSION_ALL,
  634. 'size' => -1,
  635. ];
  636. }
  637. }
  638. public function versioningEnabled(): bool {
  639. if ($this->versioningEnabled === null) {
  640. $cached = $this->memCache->get('versioning-enabled::' . $this->getBucket());
  641. if ($cached === null) {
  642. $this->versioningEnabled = $this->getVersioningStatusFromBucket();
  643. $this->memCache->set('versioning-enabled::' . $this->getBucket(), $this->versioningEnabled, 60);
  644. } else {
  645. $this->versioningEnabled = $cached;
  646. }
  647. }
  648. return $this->versioningEnabled;
  649. }
  650. protected function getVersioningStatusFromBucket(): bool {
  651. try {
  652. $result = $this->getConnection()->getBucketVersioning(['Bucket' => $this->getBucket()]);
  653. return $result->get('Status') === 'Enabled';
  654. } catch (S3Exception $s3Exception) {
  655. // This is needed for compatibility with Storj gateway which does not support versioning yet
  656. if ($s3Exception->getAwsErrorCode() === 'NotImplemented') {
  657. return false;
  658. }
  659. throw $s3Exception;
  660. }
  661. }
  662. public function hasUpdated($path, $time) {
  663. // for files we can get the proper mtime
  664. if ($path !== '' && $object = $this->headObject($path)) {
  665. $stat = $this->objectToMetaData($object);
  666. return $stat['mtime'] > $time;
  667. } else {
  668. // for directories, the only real option we have is to do a prefix listing and iterate over all objects
  669. // however, since this is just as expensive as just re-scanning the directory, we can simply return true
  670. // and have the scanner figure out if anything has actually changed
  671. return true;
  672. }
  673. }
  674. }