I want trying to get file from blob storage. I got successfully from blob storage without subdirectory but when I am trying to get files from inside subdirectory then it doesn't work. In subdirectory case I use $blobName like that test/1.png
where test is folder name and 1.jpg file name.
<?php
$storageAccount = 'XXXXXXX';
$containerName = 'XXXXXXXX';
$blobName = 'test/1.png';
$account_key = 'XXXXXXXXXX';
$date = gmdate('D, d M Y H:i:s \G\M\T');
$version = "2019-12-12";
$stringtosign = "GET\n\n\n\n\n\n\n\n\n\n\n\nx-ms-date:". $date . "\nx-ms-version:".$version."\n/".$storageAccount."/".$containerName."/".$blobName;
$signature = 'SharedKey'.' '.$storageAccount.':'.base64_encode(hash_hmac('sha256', $stringtosign, base64_decode($account_key), true));
echo "\n\n" . $signature;
$header = array (
"x-ms-date: " . $date,
"x-ms-version: " . $version,
"Authorization: " . $signature
);
$url="https://$storageAccount.blob.core.windows.net/$containerName/$blobName";
$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 "\n\n" . $result;
if(curl_errno($ch)){
throw new Exception(curl_error($ch));
}
file_put_contents('D://demo//downloaded.txt', $result); // save the string to a file
curl_close($ch);
I have tested your code on my side but seems your code is all right, I can download my .jpg file which inside some subdirectory from the storage container:
I noticed in your code, you are downloading .png file
content into a .txt file
:
file_put_contents('D://demo//downloaded.txt', $result);
Maybe this is the reason that you did not get the excepted result.