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*

12/2/2006

Amazon S3 Tip, Check Availability First

Filed under: PHP — geoff @ 9:32 pm

If you’re using Amazon S3 in PHP (whether you are using the neurofuzzy s3 class or your own solution) it is a good idea to make sure you can access the service before you attempt any operations on it. If for some reason your site cannot access S3, it’s possible your server could get seriously bogged down or even crash if you aren’t careful.

In my experience, the PEAR HTTPrequest class doesn’t timeout properly out of the box. This means that if you can’t reach S3, HTTP requests will remain active, keeping processes running and possibly MySQL connections open. If you are running scripts that access S3 on a cron, or actively on your site’s pages, your request queues can back up, overloading your server.

I can get even hairier than that. If you are running scripts that pipe images or other data from S3 through your site as a proxy (you may do this to mask the S3 urls or to get around Flash security restrictions), it’s possible that your users will have their browser’s HTTP requests to your site back up as well. Since browsers have limits on concurrent connections to a single domain, this will effectively cause your site to lock up for the user. Very bad indeed.

If you are running a backend script that works via HTTPrequest, or any script where you’ve turned off timeouts, it’s imperative to check to make sure you can get to S3. One solution is to upload a small, publicly available resource to a bucket, and then when you want to check S3, just try to grab the file.

Put something like this at the beginning of your script:

// make sure S3 is accessible
if (!@file_get_contents("http://s3.amazonaws.com/mybucket/…
… myresource.ext")) {
      die("Server can't see S3!");
}

If you are piping images through your site from S3 to your users, use cURL instead of readfile. cURL’s timeouts are more reliable in my experience:

$location = "http://s3.amazonaws.com/mybucket/myresource.ext";
$ch = curl_init();
$timeout = 5; // set timeout in seconds - zero for no timeout
curl_setopt ($ch, CURLOPT_URL, $location);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);

It’s also a good idea to close any MySQL connections you may have open before you transfer any large files.

These ideas are based on my own experiences, and I’m sure that there are others out there who may have better ideas. Feel free to post your ideas in the comments. Thanks!

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

2 Comments »

  1. Hi

    I have been reading your Amazon S3 posts which I find very informative. I have developed a web application for media professionals to work with video , audio, images and docs. The app is PHP / MySQL. The site is a business site and will not have more than 5000 users if that. I have been looking into S3 as a storage solution and to build an api to my app.

    I am interested in your thoughts on this in terms of will it seamlessly work with my app and not lead to problems, like delays in pulling the media
    into my app.

    Thanks,

    Andy

    Comment by andy — 2/11/2007 @ 4:28 pm

  2. Andy,

    I’ve not noticed any huge delays from S3, although it is admittedly slower than storing everything local. I have heard of problems posting large files, like video. I’ve not done this, personally.

    Pulling stuff down can be as simple as just HTTP calls if you are not using ACL.

    -g

    Comment by geoff — 2/14/2007 @ 5:44 pm

RSS feed for comments on this post. TrackBack URI

Leave a comment

 

Powered by WordPress