Home » Php » amazon s3 – How can I filter S3 objects by size using AWS SDK for PHP v3 Aws/ResultPaginator->Search and JMESPath expression?

amazon s3 – How can I filter S3 objects by size using AWS SDK for PHP v3 Aws/ResultPaginator->Search and JMESPath expression?

Posted by: admin February 25, 2020 Leave a comment

Questions:

I need some assistance with filtering S3 results using AWS SDK for PHP v3 and JMESPath. Filtering by a number is not working with the PHP SDK as JMESPath documention and online examples suggest.

<?php
// test.php

use Aws\S3\S3Client;

// Create S3 client
$s3 = new S3Client([
    'version' => 'latest',
    'region'  => 'us-east-1'
]);

$bucket = 'my-bucket-name';
$prefix = 'path/to/my/objects';

// Call list-objects-v2
$awspaginator = $s3->getPaginator('ListObjectsV2', [
    'Bucket' => $bucket,
    'Prefix' => $prefix
]);

// Apply filter to paginator
$jmes = "reverse(Contents[?Size>`0`].{Key: Key, Date: LastModified, Size: Size}) | [-10:]";
$results = $awspaginator->search($jmes);

// Echo results
$i = 0;
foreach ($results as $result) {
    echo "\nResult: " . print_r($result);
    $i++;
}
echo "\nCount: " . $i . PHP_EOL;
?>

This outputs Count: 0

But if I replace Size> 0 with StorageClass==’STANDARD’ I get the 10 most recent objects as expected. NOTE that the zero is surrounded by backticks but I’m struggling with creating a literal backtick here.

I’ve attempted the following Size expressions without any luck.

  • Size>0 // returns error: unexpected number token
  • Size>’0′ // succeeds: returns no results
  • Size>0 // succeeds: returns no results
  • Size!=0 // returns results but does not filter out zero size objects
  • Size!=\”0\” // returns results but does not filter out zero size objects

Note that the s2api query works just fine so this seems to be something to do with the PHP SDK Search method.

--bucket my-bucket-name \
--prefix path/to/my/objects \
--query "reverse(Contents[?Size>\`0\`].{Key: Key, Date: LastModified, Size: Size}) | [-10:]"

Any help is appreciated!

How to&Answers: