Skip to content

Commit

Permalink
S3 pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
fogelito committed Sep 18, 2023
1 parent 76e440f commit d116ea2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
8 changes: 5 additions & 3 deletions src/Storage/Device.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<mixed>
*/
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.
Expand Down
2 changes: 1 addition & 1 deletion src/Storage/Device/Local.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 15 additions & 4 deletions src/Storage/Device/S3.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<mixed>
*
* @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
{
Expand Down
19 changes: 19 additions & 0 deletions tests/Storage/S3Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit d116ea2

Please sign in to comment.