Search code examples
phpamazon-s3aws-php-sdk

php s3 sdk - how to get object's latest version


I'm trying to get latest version of a given object. I tried using this function listObjectVersions but couldn't get it to work like I want. It lists all the files, with their versions, on my bucket.

$fileVersion = $s3Client->listObjectVersions([
    'Bucket' => 'bucketname',
    'Key' => 'folder/file.jpeg' // get all versions of this file. though, this doesn't work
  ]);

Solution

  • Listing objects in your buckets

    $iterator = $client->getIterator('ListObjects', array(
        'Bucket' => $bucket,
        'Prefix' => 'foo'
    ));
    
    foreach ($iterator as $object) {
        echo $object['Key'] . "\n";
    }
    

    More : https://docs.aws.amazon.com/aws-sdk-php/v2/guide/service-s3.html#listing-your-buckets