The following examples illustrates how to retrieve all objects of a given S3 bucket. All you need to replace in the snippet are the $bucket, $key and $secret variables.
Retrieving all objects from a bucket in S3
<?php
require_once 'Services/Amazon/S3.php';
$key = 'your key';
$secret = 'your secret';
$bucket = 'foobar';
$s3 = Services_Amazon_S3::getAccount($key, $secret);
$bucket = $s3->getBucket($bucket);
echo '<ul>';
foreach ($bucket->getObjects() as $object) {
echo "<li>{$object->name}</li>";
}
echo '</ul>';
?>
This example details how to retrieve the meta of the objects - for example, size, mimetype, etc..
Retrieving all objects meta data from a bucket in S3
<?php
require_once 'Services/Amazon/S3.php';
$key = 'your key';
$secret = 'your secret';
$bucket = 'foobar';
$s3 = Services_Amazon_S3::getAccount($key, $secret);
$bucket = $s3->getBucket($bucket);
echo '<ul>';
foreach ($bucket->getObjects() as $object) {
$object->load(Services_Amazon_S3_Resource_Object::LOAD_METADATA_ONLY);
echo "<li>{$object->name}: {$object->size} bytes ({$object->contentType})</li>";
}
echo '</ul>';
?>
The following example illustrates how to create an access URL to the objects, with a TTL (time to live, expire time).
Retrieving all objects from a bucket in S3
<?php
require_once 'Services/Amazon/S3.php';
$key = 'your key';
$secret = 'your secret';
$bucket = 'foobar';
$s3 = Services_Amazon_S3::getAccount($key, $secret);
$bucket = $s3->getBucket($bucket);
echo '<ul>';
foreach ($bucket->getObjects() as $object) {
$url = $object->getSignedUrl(120); // expire in 2 minutes
echo "<li>{$object->name}";
echo '<a href="' . $url . '">download it (link is valid for 2 minutes)</a>";
echo "</li>";
}
echo '</ul>';
?>