Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS SDK for PHP - How to get creation date of a file

I have working code which returns all the files under s3 bucket. I have to get today's uploaded files for further processing.

Code to get files :

use Aws\S3\S3Client;
$s3Client = S3Client::factory(array(
    'region'  => 'us-east-1',
    'version' => '2006-03-01',
    'credentials' => array(
    'key'    => 'XXXX',
    'secret' => 'YYYYYYYY'

)
));

$iterator = $s3Client->getIterator('ListObjects', array(
    'Bucket' => 'mybucket',    
    'Prefix' => 'adityamusic',
    'Suffix' => '.xlsx',
    ), array(
    'limit'     => 999,
    'page_size' => 100
));

foreach ($iterator as $object) {
    print_r($object['LastModified']); 
    print_r($object['LastModified']['date']);   //this gives error
}

print_r($object['LastModified']) outputs as :

Array
(
   [Key] => mymusic/
   [LastModified] => Aws\Api\DateTimeResult Object
       (
           [date] => 2016-08-03 06:20:31
           [timezone_type] => 2
           [timezone] => Z
       )

   [ETag] => "sadfasdf2342"
   [Size] => 0
   [StorageClass] => STANDARD
   [Owner] => Array
       (
           [DisplayName] => test
           [ID] => asdfasdfasdf
       )

)

I am not able to access date key.

like image 892
Tushar Gaurav Avatar asked Nov 29 '22 09:11

Tushar Gaurav


1 Answers

LastModified is an instance of Aws\Api\DateTimeResult class, since DateTimeResult extends \DateTime object, just use format method as you would normally do when working with standard DateTime objects.

echo $object['LastModified']->format(\DateTime::ISO8601)

Read here for more formatting options.

P.S. $object['LastModified']->date will not work because its not designed to be accessed directly.

like image 70
b.b3rn4rd Avatar answered Dec 06 '22 07:12

b.b3rn4rd