Question:
PHP cURL to upload video to azure blob storage

Problem

I have issues creating a new/simple PHP script/function with a cURL to upload a simple file (video.mp4) from local server to Azure storage - blob container using an authentication key. I do not want (for other reasons) use SDK and/or multiple files/libraries. I need only one function - fileUpload - and that is it.

I am able to get the key using another function without any issue but when I try to upload the file using the below fileUpload function, I am facing the following error


InvalidHeaderValueThe value for one of the HTTP headers is not in the correct format. RequestId:798508a1-701e-0055-0e82-efab9f000000 Time:2023-09-25T07:34:50.5926245ZContent-Length-1


function uploadFileToAzureBlob($key, $container, $azureUrl, $uploadedFile, $fileArray){


    $url = $azureUrl.$container."/".pathinfo($uploadedFile["file"], PATHINFO_FILENAME);


    echo json_encode($uploadedFile)."<br>";

    echo json_encode($fileArray)."<br>";

    echo filesize($uploadedFile["file"])."<br>";


    $ch = curl_init();

    echo $url."<br>";


    // Open file

    $fileHandle = fopen($uploadedFile["file"], 'r');

    // Set the URL

    curl_setopt($ch, CURLOPT_URL, $url);


    // Set the HTTP method to PUT

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');


    // Set the body of the request to the contents of the file

    curl_setopt($ch, CURLOPT_INFILE, $fileHandle);


    

    $headers = array(

        "Transfer-Encoding" => "chunked"

        ,"x-ms-blob-type" => "BlockBlob"

        ,'x-ms-version'=>'2017-11-09'

        , "x-ms-date" => gmdate('D, d M Y H:i:s T', time())

        ,"Content-Type" => $fileArray["type"]

        ,"Content-Length" => filesize($uploadedFile["file"])

        ,"Authorization"=>$key);


    echo json_encode($headers)."<br>";

    curl_setopt($ch, CURLOPT_HTTPHEADER, array_map(function($v,$k){

                                                        return $k.":".$v;

                                                    },$headers,array_keys($headers)));

                                                echo $fileArray["size"]."<br>";

    // Set the size of the file for the Content-Length header

    curl_setopt($ch, CURLOPT_INFILESIZE, $fileArray["size"]);

    curl_setopt($ch, CURLOPT_UPLOAD, true); 

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

    // Execute the CURL session

    $response = curl_exec($ch);

    echo json_encode($response)."<br>";

    echo "curl:::: " .json_encode(curl_getinfo($ch))."<br>";

    // Close file and CURL session

    fclose($fileHandle);

    curl_close($ch);


    return $response;

}



Answer:

To upload video from local to Azure blob storage using Put blob request you can use the below PHP code.


I used a 5MB sample video file to upload Azure blob storage.


Code:

<?php


$storageAccountname = 'youraccount name';

$accesskey = "youraccount key";

$filepath = realpath('./test.mp4');

$containerName = 'your-container-name';

$blobName = 'demo.mp4';


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


function uploadBlob($filepath, $storageAccountname, $containerName, $blobName, $URL, $accesskey) {


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

    $handle = fopen($filepath, "r");

    $fileLen = filesize($filepath);


    $headerResource = "x-ms-blob-cache-control:max-age=3600\nx-ms-blob-type:BlockBlob\nx-ms-date:$Date\nx-ms-version:2019-12-12";

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


    $arraysign = array();

    $arraysign[] = 'PUT';               /*HTTP Verb*/  

    $arraysign[] = '';                  /*Content-Encoding*/  

    $arraysign[] = '';                  /*Content-Language*/  

    $arraysign[] = $fileLen;            /*Content-Length (include value when zero)*/  

    $arraysign[] = '';                  /*Content-MD5*/  

    $arraysign[] = 'video/mp4';   /*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-blob-cache-control: max-age=3600',

        'x-ms-blob-type: BlockBlob',

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

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

        'Content-Type: video/mp4',

        'Content-Length: ' . $fileLen

    ];


    $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);

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");

    curl_setopt($ch, CURLOPT_INFILE, $handle); 

    curl_setopt($ch, CURLOPT_INFILESIZE, $fileLen); 

    curl_setopt($ch, CURLOPT_UPLOAD, true); 

    $result = curl_exec($ch);


    echo ('Uploaded successfully<br/>');

    print_r($result);


    curl_close($ch);

}


uploadBlob($filepath, $storageAccountname, $containerName, $blobName, $URL, $accesskey);


Console:


Output:


Portal:


Answered by: >Venkatesan

Credit:> StackOverflow


Ritu Singh

Ritu Singh

Submit
0 Answers