Skip to content

Instantly share code, notes, and snippets.

@jd-war-eagle
Created October 14, 2016 16:56
Show Gist options
  • Save jd-war-eagle/422e06eebb37c2a2cca44af0b9970e7d to your computer and use it in GitHub Desktop.
Save jd-war-eagle/422e06eebb37c2a2cca44af0b9970e7d to your computer and use it in GitHub Desktop.
PHP - Get most recently modified file from s3 bucket
<?php
require './aws.phar';
use Aws\S3\S3Client;
$s3 = S3Client::factory(array(
'key' => 'YOUR_KEY',
'secret' => 'YOUR_SECRET',
'region' => 'YOUR_REGION',
'signature' => 'v4',
'version' => 'latest',
));
$bucket = 'YOUR_BUCKET';
$objects = $s3->getIterator('ListObjects', array(
"Bucket" => $bucket,
));
// initialize with a 0 DateTime
$most_recent = array('LastModified' => date(0));
foreach ($objects as $object) {
// DateTime result
$last_modified = $object['LastModified'];
if ($last_modified > $most_recent['LastModified']) {
$most_recent = $object;
}
}
var_dump($most_recent);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment