Search code examples
phpamazon-web-servicesamazon-s3aws-php-sdk

sorting an S3 bucket by last updated date


According to https://stackoverflow.com/a/65675842/569976 , you can use the AWS S3 CLI client to sort the results of a list-objects-v2 by the last modified date, as follows:

aws s3api list-objects --bucket bucketname --query 'sort_by(Contents, &LastModified)[-1].Key' --output text

My question is... how do you do that with the PHP client / Aws\S3\S3Client::listObjects?

https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-s3-2006-03-01.html#listobjects mentions a bunch of options you can pass to it and none of them are "query":

$result = $client->listObjects([
    'Bucket' => '<string>', // REQUIRED
    'Delimiter' => '<string>',
    'EncodingType' => 'url',
    'ExpectedBucketOwner' => '<string>',
    'Marker' => '<string>',
    'MaxKeys' => <integer>,
    'OptionalObjectAttributes' => ['<string>', ...],
    'Prefix' => '<string>',
    'RequestPayer' => 'requester',
]);

Is it just not possible with the PHP API client?


Solution

  • --query is a client-side parameter supported by AWS CLI. It's not a part of the API.

    You'll need to sort the results in your PHP code.

    This parameter uses JMESPath as a scripting language for modifying the output results. If you want to completely mimic its behavior, you can use a JMESPath library such as this.