« November 2006 | Main

December 19, 2006

Amazon S3 PHP Class

A few days ago I stumbled over Amazon's Simple Storage System and started playing around with it.

I just could not find any good premade S3 PHP class that I liked so I wrote my own. The first beta version is now ready for release. I have no documentation yet so check the source, just a few basics are below.

I created 4 classes:
AmazonS3 - Base Amazon S3 class
S3Bucket - Represents an Amazon S3 Bucket
S3Object - Represents an Amazone S3 Object
S3ACL - Represents an Amazon S3 ACL Record

You can create each of those by yourself, or start with an S3 object and get a Bucket out of that, and an Object out of the Bucket... I deceided that this would be nicer for usage later on too.

Below is a sample which would create a bucket (if it does not exist yet) and add a test object to that bucket with public read access.

$s3 = new AmazonS3($awskeyid, $secretkey);
$bucket = $s3->Bucket('mytestbucket');
$object = $bucket->S3Object('test_object');
$object->SetData('This is a Test Object accessible via http://s3.amazonaws.com/mytestbucket/test_object');
$object->SetType('text/html');
$object->SetCannedACL(AS3_ACL_PUBLIC_READ);
$object->Put();

That is all that is needed. I'll be posting more updates about this hopefully soon when I find some more time.

The Classes require PHP 5, the PHP CURL extension, and the PECL HASH extension to work!

The class file is below.

Download s3.class.php

Creative Commons License
This work is licensed under a Creative Commons Attribution-Share Alike 2.0 Germany License.

Posted by Nathan on December 19, 2006 at 02:15 PM in Coding | Permalink | Comments (6) | TrackBack