Question:
How to create a new/simple PHP script/function with cURL to upload simple file (video.mp4) from local server to Azure storage

We are creating this PHP function to upload a simple MP4 video file from a local server to Azure storage. 


Here we are using a 5MB sample video file to upload Azure blob storage.


Start by the below 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

Blogs Link: 

>How to solve encoding issue when writing to a text file, with Python?

>Python Error Solved: pg_config executable not found

>How to solve XGBoost model training fails in Python

>Python Error Solved: load_associated_files do not load a txt file

>How to build interactive_graphviz in Python


Ritu Singh

Ritu Singh

Submit
0 Answers