From d116ea2db84cfdd61207e416c17064889bd3a37c Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 18 Sep 2023 12:57:11 +0300 Subject: [PATCH] S3 pagination --- src/Storage/Device.php | 8 +++++--- src/Storage/Device/Local.php | 2 +- src/Storage/Device/S3.php | 19 +++++++++++++++---- tests/Storage/S3Base.php | 19 +++++++++++++++++++ 4 files changed, 40 insertions(+), 8 deletions(-) diff --git a/src/Storage/Device.php b/src/Storage/Device.php index 74617c9e..f71b524b 100644 --- a/src/Storage/Device.php +++ b/src/Storage/Device.php @@ -270,10 +270,12 @@ abstract public function getPartitionTotalSpace(): float; /** * Get all files and directories inside a directory. * - * @param string $dir Directory to scan - * @return array + * @param string $dir Directory to scan + * @param int $keys + * @param string $continuationToken + * @return array */ - abstract public function getFiles(string $dir): array; + abstract public function getFiles(string $dir, int $keys = 1000, string $continuationToken = ''): array; /** * Get the absolute path by resolving strings like ../, .., //, /\ and so on. diff --git a/src/Storage/Device/Local.php b/src/Storage/Device/Local.php index 6ef23b63..294c3ea7 100644 --- a/src/Storage/Device/Local.php +++ b/src/Storage/Device/Local.php @@ -507,7 +507,7 @@ public function getPartitionTotalSpace(): float * @param string $dir Directory to scan * @return string[] */ - public function getFiles(string $dir): array + public function getFiles(string $dir, int $keys = 1000, string $continuationToken = ''): array { if (! (\str_ends_with($dir, DIRECTORY_SEPARATOR))) { $dir .= DIRECTORY_SEPARATOR; diff --git a/src/Storage/Device/S3.php b/src/Storage/Device/S3.php index caf50c38..1f994862 100644 --- a/src/Storage/Device/S3.php +++ b/src/Storage/Device/S3.php @@ -658,24 +658,35 @@ public function getPartitionTotalSpace(): float /** * Get all files and directories inside a directory. * - * @param string $dir Directory to scan - * @return array + * @param string $dir Directory to scan + * @param int $keys + * @param string $continuationToken + * @return array * * @throws Exception */ - public function getFiles(string $dir): array + public function getFiles(string $dir, int $keys = 1000, string $continuationToken = ''): array { - $data = $this->listObjects($dir); + $data = $this->listObjects($dir, $keys, $continuationToken); + + // Set to false if all the results were returned. Set to true if more keys are available to return. $data['IsTruncated'] = $data['IsTruncated'] === 'true'; + + // KeyCount is the number of keys returned with this request. $data['KeyCount'] = intval($data['KeyCount']); + // Sets the maximum number of keys returned to the response. By default, the action returns up to 1,000 key names. + $data['MaxKeys'] = intval($data['MaxKeys']); + return $data; } /** * Get file info * + * @param string $path * @return array + * @throws Exception */ private function getInfo(string $path): array { diff --git a/tests/Storage/S3Base.php b/tests/Storage/S3Base.php index b8cd1a8d..3857eca8 100644 --- a/tests/Storage/S3Base.php +++ b/tests/Storage/S3Base.php @@ -74,6 +74,25 @@ public function testGetFiles() $this->assertArrayHasKey('Size', $file); } + public function testGetFilesPagination() + { + $path = $this->object->getPath('testing/'); + + $files = $this->object->getFiles($path, 3); + $this->assertEquals(3, $files['KeyCount']); + $this->assertEquals(1000, $files['MaxKeys']); + $this->assertEquals(true, $files['IsTruncated']); + $this->assertIsArray($files['Contents']); + $this->assertNotEmpty($files['NextContinuationToken']); + + $files = $this->object->getFiles($path, 3, $files['NextContinuationToken']); + $this->assertEquals(1, $files['KeyCount']); + $this->assertEquals(1000, $files['MaxKeys']); + $this->assertEquals(false, $files['IsTruncated']); + $this->assertIsArray($files['Contents']); + $this->assertEmpty($files['NextContinuationToken']); + } + public function testName() { $this->assertEquals($this->getAdapterName(), $this->object->getName());