From 4163ee97ef66e357c5ff15ce1a680dffdbdd9340 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Wed, 9 Sep 2020 14:29:11 +0200 Subject: [PATCH 1/2] fix s3 doesDirectoryExist check for empty directories if a directory is empty, only the 'marker' object `/` exists. since not all s3 implementations return just the prefix when listing objects by prefix, when listing objects by the folder prefix, nothing will be returned if the directory is empty. by not including the trailing slash in the prefix, the folder marked will always be returned if it exists Signed-off-by: Robin Appelman --- apps/files_external/lib/Lib/Storage/AmazonS3.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/files_external/lib/Lib/Storage/AmazonS3.php b/apps/files_external/lib/Lib/Storage/AmazonS3.php index 6838784204f73..9a3697424fd72 100644 --- a/apps/files_external/lib/Lib/Storage/AmazonS3.php +++ b/apps/files_external/lib/Lib/Storage/AmazonS3.php @@ -159,7 +159,7 @@ private function doesDirectoryExist($path) { try { $result = $this->getConnection()->listObjects([ 'Bucket' => $this->bucket, - 'Prefix' => rtrim($path, '/') . '/', + 'Prefix' => rtrim($path, '/'), 'MaxKeys' => 1, ]); $this->directoryCache[$path] = $result['Contents'] || $result['CommonPrefixes']; From 2e3539863e1050b15d15c2c4f8f9dff1c26045ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Wed, 9 Sep 2020 15:19:04 +0200 Subject: [PATCH 2/2] Set delimiter so that the CommonPrefixes response can properly be determined MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- apps/files_external/lib/Lib/Storage/AmazonS3.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apps/files_external/lib/Lib/Storage/AmazonS3.php b/apps/files_external/lib/Lib/Storage/AmazonS3.php index 9a3697424fd72..0e9c4ebc12012 100644 --- a/apps/files_external/lib/Lib/Storage/AmazonS3.php +++ b/apps/files_external/lib/Lib/Storage/AmazonS3.php @@ -161,8 +161,9 @@ private function doesDirectoryExist($path) { 'Bucket' => $this->bucket, 'Prefix' => rtrim($path, '/'), 'MaxKeys' => 1, + 'Delimiter' => '/', ]); - $this->directoryCache[$path] = $result['Contents'] || $result['CommonPrefixes']; + $this->directoryCache[$path] = ($result['Contents'][0]['Key'] === rtrim($path, '/') . '/') || $result['CommonPrefixes']; } catch (S3Exception $e) { if ($e->getStatusCode() === 403) { $this->directoryCache[$path] = false;