Amazon S3 PHP Class
I’m sure you’ve all heard about Amazon.com’s new Online Data Storage Service. I just threw together a PHP 4 compatible class for working with the Amazon S3 web service. It’s based on a proof of concept they had on their site, but it was cumbersome to use. Hopefully this will make life easier for some of us who still use PHP. It relies on the Crypt_HMAC and HTTP_Request PEAR libraries.
Comments? Post them here.
UPDATE 3/17/06: I neglected to add a note from Amazon regarding a bug in the HTTP_Request package. There’s now a copy of the note in there with info on the fix.
UPDATE 3/18/06: I found some bugs when doing enhanced operations with modified acl, contenttype, and metadata. Also, I’m now returning an array of headers on PUT operations.
UPDATE 8/26/06: Get the latest info here.
Download (ZIP file) » Amazon S3 PHP Wrapper Class
There’s no documentation, and no real help yet, but it should be fairly self-exlanatory. See the API documentation for REST on the Amazon site for for info.
You’ll need to start buy making some buckets for yourself to put stuff in (see putBucket). Here’s some sample usage for working with objects:
require_once("s3.class.php");
$object_id = "l_2024";
$filePath = "path/to/my/file";
$bucket = "my_bucket";
// PUT OBJECT
if ($data = file_get_contents($filePath)) {
$transfer = new s3();
echo $transfer->putObject($object_id, $bucket);
echo("done");
} else {
echo("error.");
}
// GET OBJECT
$transfer = new s3();
$data = $transfer->getObject($photo_id, $bucket);
header("content-type: ".$transfer->getResponseContentType());
echo $data;
// GET OBJECT INFO
$transfer = new s3();
$metadata = $transfer->getObjectInfo($photo_id, $bucket);
header("content-type: ".$transfer->getResponseContentType());
foreach ($metadata as $key => $val) {
echo("$key :: $val n");
}


