Amazon S3 is a great service for hosting large media files for your site. At World Reviewer we are soon going to be rolling out our own video hosting for tour operator’s videos. Video hosting has been an interesting little project, and S3 has really made it easier for us (not to mention cheaper).
S3 by default hosts files in the US, but it is possible to host content in Europe. One thing we noticed early on, was that videos did not always stream as smoothly when hosted on the American servers and viewed in the UK (and we assume other European countries). Because of this we wanted to offer “geo-targeting”, where we can host the files twice, once in America and once in Europe and choose the best based on the user’s location. Of course, this means we have to pay for storage twice, but typically bandwidth will be the most expensive element of you bill, so a few more cents for extra storage might not be a problem. Well thats how it is for us, you mileage may vary!
It would be great if S3 had geo-targeting by default, but it does not, so how can we offer it?
Actually its quite simple. Basically we will set up two S3 buckets, say “us.media.worldreviewer.com” and “eu.media.worldreviewer.com” being the American and European buckets respectivly. We then decide between the two bucket urls based on the users location.
Geo-coding of IP addresses is very easy and free (well at least down to the country level). There are a variety of webservices (like hostip) however, we wanted something that could run locally, to cut down the delay. Maxmind offer a free PHP IP to country database called GEO lite (they also sell licenses for a more accurate version, but this is good enough for us). You need to download both the data file (GeoIP.dat) and the PHP library file (geoip.php) from that page. Once you have them you can get the country code for any IP address like this:
geoip_country_code_by_addr('212.140.189.10'); //returns 'GB'
Easy! Now all we need to do is map European countries to the European bucket url and all others to the American url. This function in this file does just that. Useage:
$bucket = '.media.worldreviewer.com';
$bucket = getAmazonS3LocalServer().$bucket;
You can then pass the bucket to your HTML layer to load the media files from the right place. The GeoIP lookup is quite quick, but you are probably better off storing the result in the user’s session, so that you only have to look it up when the visitor first views a video.