I think I have Amazon S3/CDN integration with Caribinner working pretty well now. Here are the steps I took:
1. Add an S3 library to your application/libraries folder. I used the one available here: http://undesigned.org.za/2007/10/22/amazon-s3-php-class and then customized it so that it pulled the variables from my config file. The only thing I changed in the S3.php file (which is the one to add to application/libraries) was the __construct on line 57 so that it looked like this:
public function __construct($accessKey = null, $secretKey = null, $useSSL = true) {
//Start Customizations
$this->CI = get_instance();
$accessKey = $this->CI->config->item('key_id');
$secretKey = $this->CI->config->item('secret_key');
//End Customizations
if ($accessKey !== null && $secretKey !== null)
self::setAuth($accessKey, $secretKey);
self::$useSSL = $useSSL;
}
2. Next, update the carabiner config file with the following variables
$config['key_id'] = 'XXXXXXXX'; //Your Amazon key_id
$config['secret_key'] = 'XXXXX'; //Your Amazon secret_key
$config['cdn_bucket'] = 'yourcdnbucket'; //The bucket that will act as your CDN
$config['use_cdn'] = TRUE; //Enabling the CDN functions in carabiner
$config['cdn_url'] = 'http://cdn.example.com/'; //The URL used to access your CDN
3. Finally, modify the carabiner library in two spots:
The _cache function should look like this:
private function _cache($filename, $file_data)
{
$filepath = $this->cache_path . $filename;
$success = file_put_contents( $filepath, $file_data );
if($success) :
log_message('debug', 'Carabiner: Cache file '.$filename.' was written to '.$this->cache_path);
if($this->CI->config->item('use_cdn') == TRUE)
{
//If it's a production environment, move the file to the Amazon S3 CDN bucket
if($this->dev == FALSE)
{
$this->CI->load->library('S3');
$this->CI->s3->putObjectFile($filepath, $this->CI->config->item('cdn_bucket'), $this->cache_dir.$filename, S3::ACL_PUBLIC_READ);
}
}
return TRUE;
else :
log_message('error', 'Carabiner: There was an error writing cache file '.$filename.' to '.$this->cache_path);
return FALSE;
endif;
}
The _tag function should look like this:
private function _tag($flag, $ref, $cache = FALSE, $media = 'screen')
{
switch($flag){
case 'css':
$dir = ( $this->isURL($ref) ) ? '' : ( ($cache) ? $this->cache_uri : $this->style_uri );
if($this->CI->config->item('use_cdn') === TRUE && $this->dev == FALSE)
{
$dir = $this->CI->config->item('cdn_url').$this->cache_dir;
}
return '<link type="text/css" rel="stylesheet" href="'.$dir.$ref.'" media="'.$media.'" />'."\r\n";
break;
case 'js':
$dir = ( $this->isURL($ref) ) ? '' : ( ($cache) ? $this->cache_uri : $this->script_uri );
if($this->CI->config->item('use_cdn') === TRUE && $this->dev == FALSE)
{
$dir = $this->CI->config->item('cdn_url').$this->cache_dir;
}
return '[removed]CI->config->item('charset').'">[removed]'."\r\n";
break;
}
}
That should be it. Now when you are in a production environment, anytime a new cache file is created it will automatically be loaded into Amazon S3, which can act as a CDN. Then, on the site, the files use the CDN domain instead of the normal domain. Let me know if you have suggestions on how to improve this.