Norri's Simple Machines Forum CDN

I'm experimenting with the following code which will basically mirror my attachments and avatars on my forum onto my Amazon S3 account and serve them from there on subsequent requests.  If you want to do something similar (it can save a LOT of bandwidth!), copy this code in after around line 1047, in Sources/Display.php...

NB:  This code requires the S3.php file available here.

<br />/** Norri's CDN **/
<br />  $bucket = "bucketname";
<br />  $prefix = "cdn";
<br />  $cdnurl = "http://s3.amazonaws.com/$bucket/$prefix";
<br />  // redirect if CDN version exists
<br />  $cdnfileurl = "{$cdnurl}/{$real_filename}";
<br />  if (fopen($cdnfileurl, "r") !== false) {
<br />    header("Location: {$cdnfileurl}"); exit;
<br />
<br />  // else, copy over for next time and continue loading local file
<br />  } else {
<br />    set_time_limit(0);
<br />    ignore_user_abort(1);
<br />    require_once('S3.php');
<br />    // AWS access info
<br />    if (!defined('awsAccessKey')) define('awsAccessKey', '********************');
<br />    if (!defined('awsSecretKey')) define('awsSecretKey', '****************************************');
<br />    // upload
<br />    $s3 = new S3(awsAccessKey, awsSecretKey);
<br />    $s3->putObjectFile($filename, $bucket, urlencode($prefix."/".$real_filename), S3::ACL_PUBLIC_READ);
<br />  }
<br />/** /Norri's CDN **/
<br />



Next I'm going to look at probably a whole TWO lines to add to my .htaccess file to redirect direct requests for images in my forum theme to my S3 Amazon account.