topical media & game development

talk show tell print

mashup-delicious-11-delcache-create-s3-cache.php / php



  <?php
  //existing bucket name
  define('BUCKET_NAME','username_delcache');
  
  //access and secret keys for your s3 account
  define('S3_ACCESS_KEY','accesskey');
  define('S3_SECRET_KEY','secretkey');
  
  // pear installed goodness
  require_once 'Crypt/HMAC.php';
  require_once 'HTTP/Request.php';
  
  //other s3 settings
  define('S3_URL',"http://s3.amazonaws.com/");
  define('ACL_SETTING','public-read');
  
  //the date and time in rfc 822
  rfc_822_datetime = date("r");
  
  //you del.icio.us RSS feed
  define('MY_FEED_URL','http://del.icio.us/rss/YOURNAME');
  
  //bring in magpie goodness
  require_once('lib/rss_fetch.inc');
  
  //set cache directory
  define('MAGPIE_CACHE_DIR', 'cache');
  
  rss = fetch_rss(MY_FEED_URL);
  
  //create and/or update the cache for each item
  foreach (rss->items as item) {
      file_key = md5(item['link']);
  
      //get the contents of the file you want to cache
      req =& new HTTP_Request(item['link']);
      req->sendRequest();
      response_code = req->getResponseCode();
      file_content_type = req->getResponseHeader('content-type');
      file_contents = req->getResponseBody();
  
      //if there is a response body, create the cache
      if(file_contents != '')
      {    
          //assemble your s3 signature
          s3_signature = 
              "PUT\n\n".content_type."\n".rfc_822_datetime
              ."\nx-amz-acl:".ACL_SETTING."\n/".BUCKET_NAME."/".file_key;
          hasher =& new Crypt_HMAC(S3_SECRET_KEY, "sha1");
          signature = hex2b64(hasher->hash(s3_signature));
  
          //make the request to create the file in the bucket
          s3req =& new HTTP_Request(S3_URL.BUCKET_NAME."/".file_key);
          s3req->setMethod('PUT');
          s3req->addHeader("content-type", content_type);
          s3req->addHeader("Date", rfc_822_datetime);
          s3req->addHeader("x-amz-acl", ACL_SETTING);
          s3req->addHeader("Authorization", "AWS " . S3_ACCESS_KEY . ":" .
              signature);
          s3req->setBody(file_contents);
          s3req->sendRequest();
          
          if (s3req->getResponseCode() == 200) {
              echo "Cache updated for ".item['link']." (".file_key.")\n";
          } else {
              echo "Problem updating cache for ".item['link']." (".file_key.") - Status was ".s3req->getResponseCode()."\n";
          }
  
      } else {
          //handle any snoopy errors by outputting them to the screen
          echo "Problem fetching ".item['link']." (".file_key."): "
  .req->getResponseCode()."\n";
      }
      
  }
  
  function hex2b64(str) {
      raw = '';
      for (i=0; i < strlen(str); i+=2) {
          raw .= chr(hexdec(substr(str, i, 2)));
      }
      return base64_encode(raw);
  }
  
  ?>
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.