Permalink
Browse files

invalidate() replaces invalidateObject() and supports one or many keys

  • Loading branch information...
1 parent 670ac11 commit f065b61957ef19575b449e08527011c2b54383cd @subchild committed Sep 18, 2010
Showing with 25 additions and 10 deletions.
  1. +18 −8 CloudFront.php
  2. +7 −2 example.php
View
26 CloudFront.php
@@ -33,14 +33,24 @@ function __construct($accessKeyId, $secretKey, $distributionId, $serviceUrl="htt
/**
* Invalidates object with passed key on CloudFront
- * @param $key {String} Key of object to be invalidated. With or without a leading slash.
+ * @param $key {String|Array} Key of object to be invalidated, or set of such keys
*/
- function invalidateObject($key, $debug=false){
- $key = (preg_match("/^\//", $key)) ? $key : "/" . $key;
- $date = gmdate("D, d M Y G:i:s T");
- $requestUrl = $this->serviceUrl ."2010-08-01/distribution/" . $this->distributionId . "/invalidation";
- $body = "<InvalidationBatch><Path>".$key."</Path><CallerReference>".time()."</CallerReference></InvalidationBatch>";
- $req = & new HTTP_Request($requestUrl);
+ function invalidate($keys, $debug=false){
+ if (!is_array($keys)){
+ $keys = array($key);
+ }
+ $date = gmdate("D, d M Y G:i:s T");
+ $requestUrl = $this->serviceUrl."2010-08-01/distribution/" . $this->distributionId . "/invalidation";
+ // assemble request body
+ $body = "<InvalidationBatch>";
+ foreach($keys as $key){
+ $key = (preg_match("/^\//", $key)) ? $key : "/" . $key;
+ $body .= "<Path>".$key."</Path>";
+ }
+ $body .= "<CallerReference>".time()."</CallerReference>";
+ $body .= "</InvalidationBatch>";
+ // make and send request
+ $req = & new HTTP_Request($requestUrl);
$req->setMethod("POST");
$req->addHeader("Date", $date);
$req->addHeader("Authorization", $this->makeKey($date));
@@ -61,7 +71,7 @@ function invalidateObject($key, $debug=false){
return implode("\n",$er);
}
else {
- return ($this->responseCode == 201);
+ return ($this->responseCode === 201);
}
}
View
9 example.php
@@ -7,16 +7,21 @@
$distributionId = "";
$key = ""; // String representing the existing CloudFront object to invalidate
+
$cf = new CloudFront($keyId, $secretKey, $distributionId);
?>
<html>
+<head>
+<style> textarea {width:100%; height:600px; font:12px/16px consolas;} </style>
+</head>
<body>
- Key: <?=$key?><br/><hr/>
+ Key: <?=$key?><br/>
+ <hr/>
CF call:<br/>
<?/*
Passing "true" to enable debugging for the purpose of this example.
This will render the XML response.
*/?>
- <textarea style="width:100%; height:600px; font:12px/16px consolas;"><?=$cf->invalidateObject($key, true)?></textarea>
+ <textarea><?=$cf->invalidate($key, true)?></textarea>
</body>
</html>

0 comments on commit f065b61

Please sign in to comment.