aboutsummaryrefslogtreecommitdiffstats
path: root/apps
diff options
context:
space:
mode:
authorRoeland Jago Douma <rullzer@users.noreply.github.com>2019-09-26 11:39:17 +0200
committerGitHub <noreply@github.com>2019-09-26 11:39:17 +0200
commite387189d4ae7de574a647b93a8fd2147e99476af (patch)
treeda77c0af0e19df387976a2e3ee27fe3bfad0277d /apps
parent1cb1b132ca4408a94dcae34ac4b431c2c45b7df0 (diff)
parent72d22a48284dd9e700d3586db65cca50e9d6f2f0 (diff)
downloadnextcloud-server-e387189d4ae7de574a647b93a8fd2147e99476af.tar.gz
nextcloud-server-e387189d4ae7de574a647b93a8fd2147e99476af.zip
Merge pull request #14913 from nextcloud/bugfix/6954/scan-external-s3
Fix directory detection for s3
Diffstat (limited to 'apps')
-rw-r--r--apps/files_external/lib/Lib/Storage/AmazonS3.php55
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) {