diff options
author | Roeland Jago Douma <rullzer@users.noreply.github.com> | 2019-09-30 17:10:02 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-09-30 17:10:02 +0200 |
commit | 028568c4b8978f6e38a4845fdf9165041a123623 (patch) | |
tree | 0670ba321584588706bd6998535829fd1294fbe4 /apps/files_external | |
parent | e9b27b2e9816c982e5a0f9bd74e54f94bffe944b (diff) | |
parent | 32b3064b924230cadc43b5cc1954b5d6d9d49436 (diff) | |
download | nextcloud-server-028568c4b8978f6e38a4845fdf9165041a123623.tar.gz nextcloud-server-028568c4b8978f6e38a4845fdf9165041a123623.zip |
Merge pull request #17273 from nextcloud/backport/14913/stable17
[stable17] Fix directory detection for s3
Diffstat (limited to 'apps/files_external')
-rw-r--r-- | apps/files_external/lib/Lib/Storage/AmazonS3.php | 55 |
1 files changed, 51 insertions, 4 deletions
diff --git a/apps/files_external/lib/Lib/Storage/AmazonS3.php b/apps/files_external/lib/Lib/Storage/AmazonS3.php index ea7ca42dfef..c6cd6e1b2ca 100644 --- a/apps/files_external/lib/Lib/Storage/AmazonS3.php +++ b/apps/files_external/lib/Lib/Storage/AmazonS3.php @@ -61,6 +61,9 @@ class AmazonS3 extends \OC\Files\Storage\Common { /** @var CappedMemoryCache|Result[] */ private $objectCache; + /** @var CappedMemoryCache|bool[] */ + private $directoryCache; + /** @var CappedMemoryCache|array */ private $filesCache; @@ -68,6 +71,7 @@ class AmazonS3 extends \OC\Files\Storage\Common { parent::__construct($parameters); $this->parseParams($parameters); $this->objectCache = new CappedMemoryCache(); + $this->directoryCache = new CappedMemoryCache(); $this->filesCache = new CappedMemoryCache(); } @@ -98,6 +102,7 @@ class AmazonS3 extends \OC\Files\Storage\Common { private function clearCache() { $this->objectCache = new CappedMemoryCache(); + $this->directoryCache = new CappedMemoryCache(); $this->filesCache = new CappedMemoryCache(); } @@ -110,7 +115,7 @@ class AmazonS3 extends \OC\Files\Storage\Common { unset($this->objectCache[$existingKey]); } } - unset($this->filesCache[$key]); + unset($this->directoryCache[$key], $this->filesCache[$key]); } /** @@ -136,6 +141,41 @@ class AmazonS3 extends \OC\Files\Storage\Common { } /** + * Return true if directory exists + * + * There are no folders in s3. A folder like structure could be archived + * by prefixing files with the folder name. + * + * Implementation from flysystem-aws-s3-v3: + * https://github.com/thephpleague/flysystem-aws-s3-v3/blob/8241e9cc5b28f981e0d24cdaf9867f14c7498ae4/src/AwsS3Adapter.php#L670-L694 + * + * @param $path + * @return bool + * @throws \Exception + */ + private function doesDirectoryExist($path) { + if (!isset($this->directoryCache[$path])) { + // Maybe this isn't an actual key, but a prefix. + // Do a prefix listing of objects to determine. + try { + $result = $this->getConnection()->listObjects([ + 'Bucket' => $this->bucket, + 'Prefix' => rtrim($path, '/') . '/', + 'MaxKeys' => 1, + ]); + $this->directoryCache[$path] = $result['Contents'] || $result['CommonPrefixes']; + } catch (S3Exception $e) { + if ($e->getStatusCode() === 403) { + $this->directoryCache[$path] = false; + } + throw $e; + } + } + + return $this->directoryCache[$path]; + } + + /** * Updates old storage ids (v0.2.1 and older) that are based on key and secret to new ones based on the bucket name. * TODO Do this in an update.php. requires iterating over all users and loading the mount.json from their home * @@ -294,7 +334,9 @@ class AmazonS3 extends \OC\Files\Storage\Common { // sub folders if (is_array($result['CommonPrefixes'])) { foreach ($result['CommonPrefixes'] as $prefix) { - $files[] = substr(trim($prefix['Prefix'], '/'), strlen($path)); + $directoryName = trim($prefix['Prefix'], '/'); + $files[] = substr($directoryName, strlen($path)); + $this->directoryCache[$directoryName] = true; } } if (is_array($result['Contents'])) { @@ -392,8 +434,13 @@ class AmazonS3 extends \OC\Files\Storage\Common { public function is_dir($path) { $path = $this->normalizePath($path); + + if (isset($this->filesCache[$path])) { + return false; + } + try { - return $this->isRoot($path) || $this->headObject($path . '/'); + return $this->isRoot($path) || $this->doesDirectoryExist($path); } catch (S3Exception $e) { \OC::$server->getLogger()->logException($e, ['app' => 'files_external']); return false; @@ -411,7 +458,7 @@ class AmazonS3 extends \OC\Files\Storage\Common { if (isset($this->filesCache[$path]) || $this->headObject($path)) { return 'file'; } - if ($this->headObject($path . '/')) { + if ($this->doesDirectoryExist($path)) { return 'dir'; } } catch (S3Exception $e) { |