topical media & game development

talk show tell print

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



  <?php
  //uncomment if your PEAR path is different
  #set_include_path(get_include_path() . PATH_SEPARATOR . "/usr/lib/php");
  
  //your bucket name that must be unique throughout ALL s3 users
  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 settings
  define('S3_URL',"http://s3.amazonaws.com/");
  define('ACL_SETTING','public-read'); //this will let you browse to data
  
  //the date and time in rfc 822
  rfc_822_datetime = date("r");
  
  //content type can be empty for creating a bucket
  content_type = ''; //can be empty for creating a bucket but not later
  
  //assemble your s3 signature
  s3_signature = "PUT\n\n".content_type."\n".rfc_822_datetime."\nx-amz-acl:".ACL_SETTING."\n/".BUCKET_NAME."/";
  hasher =& new Crypt_HMAC(S3_SECRET_KEY, "sha1");
  signature = hex2b64(hasher->hash(s3_signature));
  
  //make the request to create the bucket
  s3req =& new HTTP_Request(S3_URL . BUCKET_NAME."/");
  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->sendRequest();
  
  print s3req->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.