Created
October 14, 2016 16:56
-
-
Save jd-war-eagle/422e06eebb37c2a2cca44af0b9970e7d to your computer and use it in GitHub Desktop.
PHP - Get most recently modified file from s3 bucket
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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