I would like to move away from an external library I’m using (succesfully) to access some files on azure files.
The goal is to have “simple” and under control code. I just need to read some text files, nothing fancy.
I’m trying to fix the below code (circulating on the net) but I’m getting the nasty message:
AuthenticationFailed Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature. RequestId:333b3e7c-401a-0076-2031-b36e2a000000 Time:2024-05-31T08:04:47.3863148Z
The MAC signature found in the HTTP request ‘sdfoisiWAIPUnjOHSoiOSDPWhdsojfs==’ is not the same as any computed signature. Server used following string to sign: ‘GET x-ms-date:Fri, 31 May 2024 08:04:47 GMT x-ms-file-range:bytes=0- x-ms-version:2019-02-02 /xxxx/file.txt’.
Anyone have experience with this?
I’m using PHP 8.3.7
Many thanks
<?php
$storageAccount = 'xxxxx';
$containerName = 'xxxxxx';
$fileName = 'xxx/xxxx/file.txt';
$account_key = 'xxxxxxxxxxxxxxxxxxxxxxxxx';
$date = gmdate('D, d M Y H:i:s GMT');
$version = "2019-12-12";
$stringtosign = "GETnnnnnnnnnnnnx-ms-date:". $date . "nx-ms-version:".$version."n/".$storageAccount."/".$containerName."/".$fileName;
$signature = 'SharedKey'.' '.$storageAccount.':'.base64_encode(hash_hmac('sha256', $stringtosign, base64_decode($account_key), true));
echo "nn" . $signature;
$header = array (
"x-ms-date: " . $date,
"x-ms-version: " . $version,
"Authorization: " . $signature
);
$url = 'https://' . $storageAccount . '.file.core.windows.net/' . $containerName . '/' . $fileName;
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, 'GET' );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header);
curl_exec ( $ch );
$result = curl_exec($ch);
echo "nn" . $result;
if(curl_errno($ch)){
throw new Exception(curl_error($ch));
}
curl_close($ch);
?>
I tried different combinations of the signature.
Simius is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.