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 / AwsS3S3Client::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?
2
Answers
--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.
As others have stated,
query
is not part of API parameters; instead you can useJMESPath
which enables you to declaratively specify how to extract elements from a JSON document. The AWS SDK for PHP already has a dependency on jmespath.php.The
AwsResultInterface
interface has asearch($expression)
method that extracts data from a result model based on a JMESPath expression.