Script to fetch your s3 objects

I thought it’d be useful for a small script (in this case php) to fetch your s3 objects. ┬áThere is a cost involved for each download and API query but this should help those of you whom use s3 for streaming fetch these assets for your website or otherwise. ┬áSimply download the AWS PHP SDK and utilize the following:

require 'aws-autoloader.php';
use Aws\S3\S3Client;

$s3 = S3Client::factory(array(
    'key'    => "******KEY*******",
    'secret' => "******SECRET*******"
));
	  
$objects = $s3->getIterator('ListObjects', array(
		        'Bucket' => 'your.bucket.name',
				'Prefix' => 'folder/',
				'Delimiter' => '/'
		    )
		    , array(
 				'return_prefixes' => true,
			)
		);

Then if you do the following, you can obtain all folders/files for a given prefix:

$folders = array();
$files = array();
foreach ($objects as $object) {
   if(isset($object["Prefix"])){
 	$folders[] = $object["Prefix"];
   }
   else if(isset($object["Key"])){
 	$files[] = $$object["Key"];
   }
}

var_dump($folders);
var_dump($files);

Comments are closed.