diff --git a/src/future/aws/PhutilAWSEC2Future.php b/src/future/aws/PhutilAWSEC2Future.php index afa6109..b3eeef0 100644 --- a/src/future/aws/PhutilAWSEC2Future.php +++ b/src/future/aws/PhutilAWSEC2Future.php @@ -1,13 +1,12 @@ awsAccessKey = $access; $this->awsPrivateKey = $private; return $this; } public function getAWSAccessKey() { return $this->awsAccessKey; } public function getAWSPrivateKey() { return $this->awsPrivateKey; } + public function getAWSRegion() { + return $this->awsRegion; + } + + public function setAWSRegion($region) { + $this->awsRegion = $region; + return $this; + } + + public function getHost() { + $host = $this->getServiceName().'.'.$this->awsRegion.'.amazonaws.com'; + return $host; + } + public function setRawAWSQuery($action, array $params = array()) { $this->params = $params; $this->params['Action'] = $action; return $this; } - public function getAWSKeys() { - return $this->AWSKeys; - } - protected function getProxiedFuture() { if (!$this->future) { $params = $this->params; if (!$this->params) { throw new Exception("You must setRawAWSQuery()!"); } if (!$this->getAWSAccessKey()) { throw new Exception("You must setAWSKeys()!"); } $params['AWSAccessKeyId'] = $this->getAWSAccessKey(); $params['Version'] = '2011-12-15'; $params['Timestamp'] = date('c'); $params = $this->sign($params); $uri = new PhutilURI('http://'.$this->getHost().'/'); $uri->setQueryParams($params); $this->future = new HTTPFuture($uri); } return $this->future; } protected function didReceiveResult($result) { list($status, $body, $headers) = $result; try { $xml = @(new SimpleXMLElement($body)); } catch (Exception $ex) { $xml = null; } if ($status->isError() || !$xml) { if (!($status instanceof HTTPFutureResponseStatusHTTP)) { throw $status; } $params = array( 'body' => $body, ); if ($xml) { $params['RequestID'] = $xml->RequestID[0]; foreach ($xml->Errors[0] as $error) { $params['Errors'][] = array($error->Code, $error->Message); } } throw new PhutilAWSException($status->getStatusCode(), $params); } return $xml; } /** * http://bit.ly/wU0JFh */ private function sign(array $params) { $params['SignatureMethod'] = 'HmacSHA256'; $params['SignatureVersion'] = '2'; ksort($params); $pstr = array(); foreach ($params as $key => $value) { $pstr[] = rawurlencode($key).'='.rawurlencode($value); } $pstr = implode('&', $pstr); $sign = "GET"."\n". strtolower($this->getHost())."\n". "/"."\n". $pstr; $hash = hash_hmac( 'sha256', $sign, $this->getAWSPrivateKey(), $raw_ouput = true); $params['Signature'] = base64_encode($hash); return $params; } }