Search code examples
azure-blob-storage

Download blob using PHP using shared access key


I’m new to azure, I need to download a file from azure blob storage to local files using Share access key using php. I used below code to download but i’m getting invalid header.

$URL = "https://teststorage.blob.core.windows.net/$containerName/$blobName";

$Date = gmdate('D, d M Y H:i:s \G\M\T');

$headerResource = "x-ms-date:$Date\nx-ms-version:2017-11-09";

$urlResource = "/teststorage/$containerName/$blobName";

$arraysign = array();

$arraysign[] = 'GET';                          

$arraysign[] = $headerResource;     

$arraysign[] = $urlResource;     

$str2sign = implode("\n", $arraysign);

$sig = base64_encode(hash_hmac('sha256', urldecode(utf8_encode($str2sign)), base64_decode($accessKey), true));

$authHeader = "SharedKey teststorage:$sig";

$headers = [

    'Authorization: ' . $authHeader,

    'x-ms-date: ' . $Date,

    'x-ms-version: 2017-12-12',

];

Please help me with that provide me an answer.

I need to download file from azure blob storage using php.


Solution

  • I need to download a file from Azure blob storage to local files using the Share Access key using PHP. I used the below code to download but I’m getting an invalid header.

    The error occurs maybe you were passing incorrect headers or invalid headers.

    In my environment, I have a file name sample.png in the storage account.

    Portal:

    enter image description here

    You can use the below code to download a file from Azure Blob storage to the local environment.

    Code:

    <?php
    
    $storageAccountName = 'venkat123';
    $accessKey = "xxxxxxxx";
    $containerName = 'test';
    $blobName = 'sample.png';
    
    function downloadBlob($storageAccountName, $containerName, $blobName, $accessKey) {
        $URL = "https://$storageAccountName.blob.core.windows.net/$containerName/$blobName";
    
        $Date = gmdate('D, d M Y H:i:s \G\M\T');
    
        $headerResource = "x-ms-date:$Date\nx-ms-version:2019-12-12";
        $urlResource = "/$storageAccountName/$containerName/$blobName";
    
        $arraysign = array();
        $arraysign[] = 'GET';               /*HTTP Verb*/
        $arraysign[] = '';                  /*Content-Encoding*/
        $arraysign[] = '';                  /*Content-Language*/
        $arraysign[] = '';                  /*Content-Length (include value when zero)*/
        $arraysign[] = '';                  /*Content-MD5*/
        $arraysign[] = '';                  /*Content-Type*/
        $arraysign[] = '';                  /*Date*/
        $arraysign[] = '';                  /*If-Modified-Since */
        $arraysign[] = '';                  /*If-Match*/
        $arraysign[] = '';                  /*If-None-Match*/
        $arraysign[] = '';                  /*If-Unmodified-Since*/
        $arraysign[] = '';                  /*Range*/
        $arraysign[] = $headerResource;     /*CanonicalizedHeaders*/
        $arraysign[] = $urlResource;        /*CanonicalizedResource*/
    
        $str2sign = implode("\n", $arraysign);
    
        $sig = base64_encode(hash_hmac('sha256', urldecode(utf8_encode($str2sign)), base64_decode($accessKey), true));
        $authHeader = "SharedKey $storageAccountName:$sig";
    
        $headers = [
            'Authorization: ' . $authHeader,
            'x-ms-date: ' . $Date,
            'x-ms-version: 2019-12-12',
        ];
    
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $URL);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch);
    
        if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == 200) {
            file_put_contents('downloaded.png', $result);
            echo 'Downloaded successfully and saved as "downloaded.png".';
        } else {
            echo 'Download failed.';
        }
    
        curl_close($ch);
    }
    
    downloadBlob($storageAccountName, $containerName, $blobName, $accessKey);
    

    Browser:

    enter image description here

    File:

    enter image description here

    Reference:

    Get Blob (REST API) - Azure Storage | Microsoft Learn