Skip to content

Instantly share code, notes, and snippets.

@wschwarz
Last active December 14, 2015 08:09
Show Gist options
  • Save wschwarz/5055526 to your computer and use it in GitHub Desktop.
Save wschwarz/5055526 to your computer and use it in GitHub Desktop.
Simple s3 list buckets script using phpsdk v2
<?php
require 'vendor/autoload.php';
use Aws\Common\Aws;
use Aws\S3\Exception\S3Exception;
$aws = Aws::factory('/path/config.php');
$s3v2 = $aws->get('s3');
echo "ListBuckets with SDK Version 2:\n";
echo "------------------------\n";
try {
$result = $s3v2->listBuckets();
foreach ($result['Buckets'] as $bucket) {
echo "- {$bucket['Name']}\n";
listImagesByBucket($s3v2, $bucket['Name']);
}
} catch (Aws\S3\Exception\S3Exception $e) {
echo "Request failed.\n";
}
echo "\n";
function listImagesByBucket($s3ref, $bucketName) {
try {
$result = $s3ref->listObjects(array(
'Bucket' => $bucketName,
));
// Loop through and display the part numbers
foreach ($result['Contents'] as $image) {
echo "{$image['Key']}\n";
}
} catch (S3Exception $e) {
echo "Error during S3 ListObjects operation.\n";
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment