/** * 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-v1/%s/%s/%d", $this->credentials->access_key_id, Time::bceTimeNow($timestamp), $expiration_in_seconds); $session_key = hash_hmac("sha256", $raw_session_key, $this->credentials->secret_access_key, false); $canonical_uri = 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); }
/** * @param string $path The bucket and object path. * @param array @param The query strings * * @return string The complete request url path with query string. */ private function getRequestUrl($path, $params) { $uri = Coder::urlEncodeExceptSlash($path); $query_string = implode("&", array_map(function ($k, $v) { return $k . "=" . Coder::urlEncode($v); }, array_keys($params), $params)); if (!is_null($query_string) && $query_string != "") { $uri = $uri . "?" . $query_string; } $parsed_url = parse_url($this->config['endpoint']); $port = ''; $protocol = isset($parsed_url['scheme']) ? $parsed_url['scheme'] : 'http'; if ($protocol !== 'http' && $protocol !== 'https') { throw new \InvalidArgumentException(sprintf("Invalid protocol: %s, either HTTP or HTTPS is expected.", $protocol)); } return sprintf("%s%s", $this->config['endpoint'], $uri); }
/** * Copy one object to another. * * @param string $source_bucket_name The source bucket name. * @param string $source_key The source object path. * @param string $target_bucket_name The target bucket name. * @param string $target_key The target object path. * @param mixed $headers The http request headers. * @param mixed $config The optional bce configuration, which will overwrite the * default configuration that was passed while creating BosClient instance. * * @return mixed */ public function copyObject($source_bucket_name, $source_key, $target_bucket_name, $target_key, $headers = array(), $config = array()) { if (empty($source_bucket_name)) { throw new \InvalidArgumentException('source_bucket_name should not be empty or None.'); } if (empty($source_key)) { throw new \InvalidArgumentException('source_key should not be empty or None.'); } if (empty($target_bucket_name)) { throw new \InvalidArgumentException('target_bucket_name should not be empty or None.'); } if (empty($target_key)) { throw new \InvalidArgumentException('target_key should not be empty or None.'); } list($object_headers, $has_user_metadata) = $this->prepareObjectHeaders($headers); $object_headers[HttpHeaders::BCE_COPY_SOURCE] = Coder::urlEncodeExceptSlash(sprintf("/%s/%s", $source_bucket_name, $source_key)); if (isset($object_headers[HttpHeaders::ETAG])) { $object_headers[HttpHeaders::BCE_COPY_SOURCE_IF_MATCH] = $object_headers[HttpHeaders::ETAG]; } $object_headers[HttpHeaders::BCE_COPY_METADATA_DIRECTIVE] = $has_user_metadata ? 'replace' : 'copy'; return $this->sendRequest(HttpMethod::PUT, array('bucket_name' => $target_bucket_name, 'key' => $target_key, 'headers' => $object_headers, 'config' => $config)); }