« Postfix To PHP | 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

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d83445413953ef00d83445414253ef

Listed below are links to weblogs that reference Amazon S3 PHP Class:

Comments

Good job!
But I test your class, but not worked!
1.
$s3 = new S3($awskeyid, $secretkey);
should be
$s3 = new AmazonS3($awskeyid, $secretkey);
2.
$bucket = $s3->Bucket('mytestbucket');
My script hangs in this step,seems it's looped fetch object from bucket and never stop!
ps. There are million objects in my bucket.

Posted by: Eric | Dec 20, 2006 1:56:44 PM

Hi Eric,

thanks for the comment, I did a small change to the system, can you take a look and test it? I changed the file, so just download it again.

Thanks,
Fabian

Posted by: Fabian | Dec 21, 2006 9:04:34 AM

Works like a charm and it's the first one I found that is actually easy to use. :) Now just tell me how I upload images and other files, which I am not sure of at the moment and I am a happy camper :)

Posted by: Oliver Thylmann | Dec 21, 2006 12:07:54 PM

Now works!
but some warning:

Warning: http_head(): Transferred a partial file; transfer closed with outstanding read data remaining (http://xxxxx) in /home/scripts/backupToAmazon_new.php on line 42

Thanks

Posted by: Eric | Dec 21, 2006 2:13:17 PM

sorry my mistake...
It's my code ...

Posted by: Eric | Dec 21, 2006 2:16:48 PM

My understanding is you're looking for information regarding our services

Posted by: Shawn Hickman | Jan 31, 2007 4:07:44 AM

Post a comment