I'm using Google Cloud Storages Signed URLs feature with the below code. File names without spaces work fine, but those with spaces give this error:
The request signature we calculated does not match the signature you provided.
I'm passing the file name via ajax to the below code, which then returns the link to the user. I've tried a combination of rawurlencode, urlencode & urldecode but can't seem to work it out.
Thanks in advance
$bucket = 'savoy_storage';
if (isset($_GET['file']))
{
$file = urldecode($_GET['file']);
$link = storageURL( $bucket, $file);
echo $link;
}
//https://groups.google.com/forum/#!topic/google-api-php-client/kvN-yoyzf_w
function storageURL( $bucketName, $file) {
$expiry = time() + 3600;
$accessId = '1234@developer.gserviceaccount.com';
$stringPolicy = "GET\n\n\n".$expiry."\n/".$bucketName."/".$file;
$fp = fopen('key.pem', 'r');
$priv_key = fread($fp, 8192);
fclose($fp);
$pkeyid = openssl_get_privatekey($priv_key,"password");
if (openssl_sign( $stringPolicy, $signature, $pkeyid, 'sha256' )) {
$signature = urlencode( base64_encode( $signature ) );
return 'https://'.$bucketName.'.commondatastorage.googleapis.com/'.$file.'?
GoogleAccessId='.$accessId.'&Expires='.$expiry.'&Signature='.$signature;
}
}
If it helps here is the returned XML from the link:
<Error>
<Code>SignatureDoesNotMatch</Code>
<Message>
The request signature we calculated does not match the signature you provided. Check your Google secret key and signing method.
</Message>
<StringToSign>
GET 1391312980 /savoy_storage/August%20promotions.xlsx
</StringToSign>
</Error>