Skip to content

Commit

Permalink
Merge pull request #613 from humanmade/backport-609-to-v3-branch
Browse files Browse the repository at this point in the history
[Backport v3-branch] Use ListObjectsV2 where applicable
  • Loading branch information
kovshenin committed Nov 28, 2022
2 parents 8ad534d + 425e5fe commit 776f6d0
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
8 changes: 4 additions & 4 deletions inc/class-stream-wrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ function () use ( $parts, $path ) {
} catch ( S3Exception $e ) {
// Maybe this isn't an actual key, but a prefix. Do a prefix
// listing of objects to determine.
$result = $this->getClient()->listObjects(
$result = $this->getClient()->listObjectsV2(
[
'Bucket' => $parts['Bucket'],
'Prefix' => rtrim( $parts['Key'], '/' ) . '/',
Expand Down Expand Up @@ -615,7 +615,7 @@ public function dir_opendir( $path, $options ) {
// the preg_match() call.
//
// Essentially, wp_unique_filename( my-file.jpg ) doing a `scandir( s3://bucket/2019/04/ )` will actually result in an s3
// listObject query for `s3://bucket/2019/04/my-file` which means even if there are millions of files in `2019/04/` we only
// listObjectsV2 query for `s3://bucket/2019/04/my-file` which means even if there are millions of files in `2019/04/` we only
// return a much smaller subset.
//
// Anyone reading this far, brace yourselves for a mighty horrible hack.
Expand All @@ -635,7 +635,7 @@ public function dir_opendir( $path, $options ) {
// Filter our "/" keys added by the console as directories, and ensure
// that if a filter function is provided that it passes the filter.
$this->objectIterator = \Aws\flatmap(
$this->getClient()->getPaginator( 'ListObjects', $op ),
$this->getClient()->getPaginator( 'ListObjectsV2', $op ),
function ( Result $result ) use ( $filterFn ) {
/** @var list<S3ObjectResultArray> */
$contentsAndPrefixes = $result->search( '[Contents[], CommonPrefixes[]][]' );
Expand Down Expand Up @@ -1087,7 +1087,7 @@ private function deleteSubfolder( string $path, array $params ) : bool {
// Use a key that adds a trailing slash if needed.
$prefix = rtrim( $params['Key'], '/' ) . '/';
/** @var array{Contents: list<array{ Key: string }>, CommonPrefixes:array} */
$result = $this->getClient()->listObjects(
$result = $this->getClient()->listObjectsV2(
[
'Bucket' => $params['Bucket'],
'Prefix' => $prefix,
Expand Down
2 changes: 1 addition & 1 deletion inc/class-wp-cli-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public function ls( array $args ) {

try {
$objects = $s3->getIterator(
'ListObjects', [
'ListObjectsV2', [
'Bucket' => strtok( S3_UPLOADS_BUCKET, '/' ),
'Prefix' => $prefix,
]
Expand Down

0 comments on commit 776f6d0

Please sign in to comment.