-1

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

icalvete
  • 987
  • 2
  • 16
  • 50
  • 1
    *"How may I order the result by LastModify? (As is posible to do with CLI)"* - by sorting / ordering whatever data you get back. *"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."* - by filtering the result based on the `StorageClass` attribute. *"I want the tags a metadata of the object. How can I get it?. Is using x-amz-optional-object-attributes? How?"* - by submitting individual GetObject requests afterwards, listting cannot return tags / metadata. – luk2302 Jul 26 '23 at 10:33
  • Hi @luk2302. Of course I can do that. But I prefer an elegant way as CLI offers. I have thousands of objects and I don't want recover and loop over all of it to do that. The last answer have all the sense. Thanks. – icalvete Jul 26 '23 at 10:42

1 Answers1

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

luk2302
  • 55,258
  • 23
  • 97
  • 137