示例#1
0
文件: Auth.php 项目: tanxiniao/bce
 /**
  * Generate BCE Authorization Signature
  *
  * @param string $method HTTP Request Method.
  * @param string $resource The resource path.
  * @param array $params The query strings.
  * @param array $headers The extra http request headers.
  * @param number $timestamp The customized timestamp, if it's 0, the will use time() instead.
  * @param number $expiration_in_seconds The valid time in seconds.
  * @param array $headers_to_sign The extra http request headers will be included in the signature
  *
  * @return string
  */
 public function generateAuthorization($method, $resource, $params = array(), $headers = array(), $timestamp = 0, $expiration_in_seconds = 1800, $headers_to_sign = null)
 {
     $raw_session_key = sprintf("bce-auth-v%s/%s/%s/%d", "1", $this->access_key, baidubce_util_Time::bceTimeNow($timestamp), $expiration_in_seconds);
     $session_key = hash_hmac("sha256", $raw_session_key, $this->access_key_secret, false);
     $canonical_uri = "/v1" . baidubce_util_Coder::urlEncodeExceptSlash($resource);
     $canonical_query_string = $this->queryStringCanonicalization($params);
     list($canonical_headers, $signed_headers) = $this->headersCanonicalization($headers, $headers_to_sign);
     $raw_signature = $method . "\n" . $canonical_uri . "\n" . $canonical_query_string . "\n" . $canonical_headers;
     $signature = hash_hmac("sha256", $raw_signature, $session_key, false);
     if (count($signed_headers) > 0) {
         return sprintf('%s/%s/%s', $raw_session_key, implode(';', $signed_headers), $signature);
     }
     return sprintf('%s//%s', $raw_session_key, $signature);
 }
示例#2
0
 /**
  * Upload a part from starting with offset.
  *
  * @param string $bucket_name The bucket name.
  * @param string $object_name The object path.
  * @param string $file_name The file which will be uploaded.
  * @param number $offset The file offset.
  * @param number $part_size The uploaded part size.
  * @param string $upload_id The uploadId returned by initiateMultipartUpload.
  * @param number $part_number The part index.
  * @param mixed $options The extra http request headers or params.
  *
  * @return mixed
  */
 public function uploadPart($bucket_name, $object_name, $file_name, $offset, $part_size, $upload_id, $part_number, $options = array())
 {
     if ($part_number < 1 || $part_number > MAX_PARTS) {
         throw new baidubce_exception_BceIllegalArgumentException("Invalid part number.");
     }
     // Only the last part's size can less than MIN_PART_SIZE, but
     // we don't know the total part count, so we have no way to do this check.
     if ($part_size >= MAX_PART_SIZE) {
         throw new baidubce_exception_BceIllegalArgumentException(sprintf("Invalid size, the maximum part size is %d", MAX_PART_SIZE));
     }
     list($headers, $params) = $this->checkOptions($options);
     $params['partnumber'] = $part_number;
     $params['uploadId'] = $upload_id;
     if (!isset($headers['Content-Type'])) {
         $headers['Content-Type'] = baidubce_util_Coder::guessMimeType($file_name);
     }
     $file_size = filesize($file_name);
     $fp = fopen($file_name, 'rb');
     try {
         $response = $this->putObjectFromHandle($bucket_name, $object_name, $fp, $file_size, $offset, $part_size, $headers, $params);
         fclose($fp);
         return $response;
     } catch (Exception $ex) {
         fclose($fp);
         throw $ex;
     }
 }
示例#3
0
 /**
  * @param string $resource The bucket and object path.
  * @param array @param The query strings
  *
  * @return string The complete request url path with query string.
  */
 private function getRequestUrl($resource, $params)
 {
     $uri = "/v1" . baidubce_util_Coder::urlEncodeExceptSlash($resource);
     // TODO(leeight)
     // k = k.replace('_', '-')
     $query_string = implode("&", array_map(array($this, 'encodeValue'), array_keys($params), $params));
     if (!is_null($query_string) && $query_string != "") {
         return $uri . "?" . $query_string;
     }
     return $uri;
 }