skip to Main Content

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


  1. --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.

    Login or Signup to reply.
  2. As others have stated, query is not part of API parameters; instead you can use JMESPath 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 a search($expression) method that extracts data from a result model based on a JMESPath expression.

    $expr = '<your_jmespath_expression>';
    $data = $result->search($expr);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search