neurofuzzy, flash game development, rich internet applications, free source code – *alt.neurotica.fuzzy*

neurofuzzy, flash game development, rich internet applications, free source code – *alt.neurotica.fuzzy*

3/17/2006

Amazon S3 PHP Class

Filed under: PHP — geoff @ 1:57 am

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");
}

19 Comments

  1. Thanks for this. One change I needed to make:

    - $stringToSign = “$verb\n\n$contenttypestring\n$httpDate\n$aclstring$metadatastring/$resource”;

    + $resourcetosign = $resource;
    + if(preg_match(‘/(.+)\?/’,$resource,$ma)) {
    + $resourcetosign = $ma[1];
    + }
    + $stringToSign = “$verb\n\n$contenttypestring\n$httpDate\n$aclstring$metadatastring/$resourcetosign”;

    Comment by Nathan Schmidt — 3/21/2006 @ 6:20 pm

  2. “Hopefully this will make life easier for some of us who still use PHP.”

    Oh my god. Now I feel like a freak. Sob! I still use PHP! So do millions of other sad people!

    What will my family say when they find out!

    Seriously, thank you for catering to an obviously unfashionable (minority? nah).

    Comment by Ralph — 3/23/2006 @ 2:09 am

  3. [...] There’s now a fairly easy to use PHP class for working with S3 that can help you start your own experiments. Take a look. [...]

    Pingback by PHPHound » Blog Archive » Amazon’s S3 Storage API and PHP — 3/23/2006 @ 10:06 am

  4. Hi there.
    I have incorporated your class into an s3 web interface. I only had to make minor modifications because it was so cleanly coded; nice work. It’s blogged here: http://axlotl.net/wp/2006/04/16/s3-web-interface/
    and there’s a note on the amazon webservices forum also.

    Thanks for your work!

    Comment by chris — 4/16/2006 @ 11:23 pm

  5. [...] A PHP 4 compatible class for working with Amazon’s S3 web service. Amazon includes some sample ruby and python bindings, and both Matt Croydon and Adrian Holovaty detail their own python-based implementations. [...]

    Pingback by philmccluskey.com codestream » Blog Archive » Links for Thursday May 11 2006 — 5/11/2006 @ 6:10 am

  6. I can’t get this class to work. It still says “SignatureDoesNotMatch” – I checked it seems to be signing properly.

    Comment by Mike — 7/19/2006 @ 4:36 am

  7. Mike,

    What operation were you running when this error came up?

    Thanks!
    Geoff

    Comment by geoff — 8/26/2006 @ 3:47 pm

  8. [...] I tried the s3 class from neurofuzzy, but I had a heck of a time installing it. I kept getting “Signature Does Not Match” errors when attempting to upload my images. So I went ahead and forged my own path. [...]

    Pingback by pairwise team blog » Blog Archive » A Simple Amazon Php s3 class — 8/28/2006 @ 11:57 am

  9. Hello

    I have used this class and example to put and get operations.but i had some problems with both operations.

    PUT:

    HTTP Request sent to: http://s3.amazonaws.com/slideroll_photos/l

    MD5 HASH OF DATA: 3gL5HZh77en8YRlkiaS/LQ==

    Setting content type to image/jpeg

    Setting acl string to private

    Signing String: PUT 3gL5HZh77en8YRlkiaS/LQ== image/jpeg Mon, 30 Oct 2006 11:22:13 GMT Standard Time x-amz-acl:private /slideroll_photos/l

    SignatureDoesNotMatchThe request signature we calculated does not match the signature you provided. Check your key and signing
    =========================
    GET::

    HTTP Request sent to: http://s3.amazonaws.com/my_bucket/1.jpg

    Signing String: GET Mon, 30 Oct 2006 11:18:01 GMT Standard Time /my_bucket/1.jpg

    AccessDeniedAccess Denied549D673484D36C6BWM/zEP/gIBbWQaedkG2jKA3HpPbmoPC8XuznFB0bCTRvOniNo2hKXMAAJ/VbwfkQ

    Warning: Cannot modify header information – headers already sent by (output started at D:\PHP\s3-php-demo\s3.class.php:583) in D:\PHP\s3-php-demo\demo.php on line 22
    AccessDeniedAccess Denied549D673484D36C6BWM/zEP/gIBbWQaedkG2jKA3HpPbmoPC8XuznFB0bCTRvOniNo2hKXMAAJ/VbwfkQ

    Comment by Raj — 10/30/2006 @ 6:15 am

  10. With your PUT, did you try using Amazon's signature tester?

    With your GET, did you create the bucket first? You need to have created a bucket, and it must belong to you. I'd imagine my_bucket was taken long ago…

    Comment by geoff — 11/2/2006 @ 9:14 am

  11. Hello

    I have successfully created the bucket and can see the getbucket function also, but when i try to put objects, I am getting the following error.

    HTTP Request sent to: http://s3.amazonaws.com/naidus/l_2025

    MD5 HASH OF DATA: MZ8OIggo0ZDwgb01FbnTzg==

    Setting content type to image/jpeg

    Setting acl string to private

    Signing String: PUT image/jpeg Mon, 06 Nov 2006 11:05:03 GMT Standard Time x-amz-acl:private /naidus/l_2025

    SignatureDoesNotMatchThe request signature we calculated does not match the signature you provided. Check your key and signing method.860AD0BXXXXXX

    Note: I have changed the line 765 in Request.php file to (HTTP_REQUEST_METHOD_POST == $this->_method && empty($this->_postData) && empty($this->_postFiles))) {

    Please help me.

    Thanks
    Raj

    Comment by Raj — 11/6/2006 @ 6:06 am

  12. I may be wrong, but it looks like you are sending the HTTP request to http://s3.amazonaws.com/naidus/l_2025. Shouldn’t you be sending it to just http://s3.amazonaws.com?

    Did you try the signature tester?

    Comment by geoff — 11/6/2006 @ 10:58 am

  13. How to you create subdirectories? If I enter say rootfile/imagefiles for a directory name, it gives me:
    MissingContentLengthYou must provide the Content-Length HTTP

    any ideas?

    Comment by patrol — 11/27/2006 @ 8:13 pm

  14. Any example of using this class to Edit a ACL of file that is already on the S3?

    for example i want to set a file to everyone to read only. then change it back to private.

    is this S3 class already able to do this, in a easy way or would it have to be done another way? thanks,
    nilesh

    Comment by Nilesh — 12/4/2006 @ 9:21 pm

  15. [...] S3 libraries for Ruby (and another one), Perl, PHP, and Java. [...]

    Pingback by Planeta Debian » Amazon S3 is everywhere — 1/16/2007 @ 3:47 am

  16. [...] S3 libraries for Ruby (and another one), Perl, PHP, and Java. [...]

    Pingback by Planeta Fedora » Amazon S3 is everywhere — 1/16/2007 @ 3:55 am

  17. I managed to clean this up so it didn't give me errors. I got as far as uploading a small jpg, but when I try to download it returns the name of the bucket at the data. Also when I try downloading the jpg from the bucket via Jolla. It's 1k in size and not readable in photoshop anymore. Any ideas?

    Comment by Grig — 11/21/2008 @ 9:26 am

  18. Hi
    I have downloaded the S3 class latest vesion(0.3.9 – 23rd Dec 2008) from the URL http://undesigned.org.za/2007/10/22/amazon-s3-php… Is this bug free and genuine version? Is any body using this S3 class?

    Comment by jivadeveloper1 — 3/2/2009 @ 3:24 pm

  19. That class was not written by me, however it looks like it supports more recently added features. Other than that I cannot speak to it.

    Comment by ggaudrea — 3/5/2009 @ 9:11 am

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

 

Powered by WordPress