Home » Web Services » Services_Amazon_S3 » Manual
A wrapper for the AWS S3 webservices.
Introduction to Services_Amazon_S3
Services_Amazon_S3 is a wrapper for AWS S3. It provides both a OO-style interface, but also an extension in order to use S3 through PHP's stream wrappers.
Example #1
Retrieving all buckets from a S3 account
The following examples illustrates how to retrieve all buckets of a given S3 account. All you need to replace in the snippet are the $key and $secret variables.
<?php
require_once 'Services/Amazon/S3.php';
$key = 'your key';
$secret = 'your secret';
$s3 = Services_Amazon_S3::getAccount($key, $secret);
echo '<ul>';
foreach ($s3->getBuckets() as $bucket) {
echo "<li>{$bucket->name}</li>";
}
echo '</ul>';
?>
Example #2 and #3
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>';
?>