icalvete
icalvete

Reputation: 1159

AWS SDK PHP 3.x: Get very LAST object in s3 bucket by last modified

I'm trying to get the last object in a bucket order by last modified using AWS SDK for PHP version 3.x.

I have seen how to doit with the AWS CLI. Here:

But I can't see how to do it with PHP SDK.

$S3Client = new Aws\S3\S3Client([
    'version'       => 'latest',
    'region'         => 'eu-west-3',
    'credentials' => [
        'key'          => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx',
        'secret'     => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    ]
]);

$startTime  = strtotime('-5 minutes');
$res = $S3Client->ListObjectsV2([
    'Bucket'                           => 'my-bucket',
    'Prefix'                           => 'objects/',
    'StartAfter'                       => $startTime
])

I'm using ListObjectsV2 method which is the recommended one.

I have three problems.

  1. How may I order the result by LastModify? (As is posible to do with CLI)

  2. I have some lifecycle policies and this affects to my search because I get some results of objects moving to GLACIER STORAGE which I do not want.

  3. I want the tags a metadata of the object. How can I get it?. Is using x-amz-optional-object-attributes? How?

By the way, using StartAfter parameter does not seems to change anything

Upvotes: -1

Views: 368

Answers (1)

luk2302
luk2302

Reputation: 57183

  1. You need to implement that sorting logic in your code, S3Client / AWS does not support that out of the box. This also means you need to list ALL objects (not just the first 1000) before you can actually sort to find out which object is the latest one.

  2. The result contains the StorageClass attribute which you can / need to filter on before e.g. sorting or further processing the results.

  3. That is not possible with listing alone. You need to submit individual GetObject requests for each object you are interested in. If you want the object metadata for many thousand objects at the same time you need to rethink your approach and should probably store such data in a properly query-able / search-able database.

Upvotes: 0

Related Questions