/** * Signs request with signature version 2 * * Only POST http method is supported * * @param Request $request Http request object * @throws QueryClientException */ protected function signRequestV2($request) { $time = time(); //Gets the http method name $httpMethod = $request->getRequestMethod(); //Gets both host and path from the url $components = parse_url($request->getRequestUrl()); $common = ['AWSAccessKeyId' => $this->awsAccessKeyId, 'SignatureVersion' => '2', 'SignatureMethod' => 'HmacSHA1', 'Timestamp' => gmdate('Y-m-d\\TH:i:s', $time) . "Z"]; $request->append($common); //Gets adjusted options $options = (new QueryString($request->getRawBody()))->toArray(); //Calculating canonicalized query string ksort($options); $c11dQueryString = http_build_query($options, null, '&', PHP_QUERY_RFC3986); $stringToSign = $httpMethod . "\n" . strtolower($components['host']) . "\n" . $components['path'] . "\n" . $c11dQueryString; switch ($common['SignatureMethod']) { case 'HmacSHA1': case 'HmacSHA256': $algo = strtolower(substr($common['SignatureMethod'], 4)); break; default: throw new QueryClientException('Unknown SignatureMethod ' . $common['SignatureMethod']); } $request->append(['Signature' => base64_encode(hash_hmac($algo, $stringToSign, $this->secretAccessKey, 1))]); $request->addHeaders(['X-Amz-Date' => gmdate(\DateTime::ISO8601, $time)]); }