Scalr\Service\Aws::getPresignedUrl PHP Method

getPresignedUrl() public method

Gets presigned url signed with aws4 version signing algorithm
public getPresignedUrl ( string $service, string $action, string $destRegion, string $objectId )
$service string Service name (ec2, s3)
$action string Action name (CopySnapshot)
$destRegion string Destination region
$objectId string Id of the snapshot, image , etc
    public function getPresignedUrl($service, $action, $destRegion, $objectId)
    {
        $canonicalizedQueryString = '';
        $time = time();
        $options = ['X-Amz-Algorithm' => 'AWS4-HMAC-SHA256', 'X-Amz-Credential' => $this->getAccessKeyId() . '/' . gmdate('Ymd', $time) . '/' . $this->getRegion() . '/' . $service . '/aws4_request', 'X-Amz-Date' => gmdate('Ymd\\THis\\Z', $time), 'X-Amz-Expires' => '86400', 'X-Amz-SignedHeaders' => 'host', 'SourceRegion' => $this->getRegion(), 'Action' => $action, 'SourceSnapshotId' => $objectId, 'DestinationRegion' => $destRegion];
        ksort($options);
        foreach ($options as $k => $v) {
            $canonicalizedQueryString .= '&' . rawurlencode($k) . '=' . rawurlencode($v);
        }
        if ($canonicalizedQueryString !== '') {
            $canonicalizedQueryString = substr($canonicalizedQueryString, 1);
        }
        $canonicalRequest = 'GET' . "\n" . "/\n" . $canonicalizedQueryString . "\n" . "host:" . $this->{$service}->getUrl() . "\n" . "\n" . "host" . "\n" . hash('sha256', '');
        $stringToSign = "AWS4-HMAC-SHA256" . "\n" . gmdate('Ymd\\THis\\Z', $time) . "\n" . gmdate('Ymd', $time) . "/" . $this->getRegion() . "/" . $service . "/aws4_request" . "\n" . hash('sha256', $canonicalRequest);
        $dateKey = hash_hmac('sha256', gmdate('Ymd', $time), "AWS4" . $this->getSecretAccessKey(), true);
        $dateRegionKey = hash_hmac('sha256', $this->getRegion(), $dateKey, true);
        $dateRegionServiceKey = hash_hmac('sha256', $service, $dateRegionKey, true);
        $signingKey = hash_hmac('sha256', 'aws4_request', $dateRegionServiceKey, true);
        $signature = hash_hmac('sha256', $stringToSign, $signingKey);
        $presignedUrl = "https://" . $this->{$service}->getUrl() . "/?" . $canonicalizedQueryString . "&X-Amz-Signature=" . rawurlencode($signature);
        return $presignedUrl;
    }