将视频上传到Azure Blob存储的PHP cURL

z2acfund  于 2023-09-29  发布在  PHP
关注(0)|答案(1)|浏览(131)

我在创建一个新的/简单的PHP脚本/函数时遇到了问题,该脚本/函数使用cURL将简单文件(video.mp4)从本地服务器上传到Azure存储-使用身份验证密钥的blob容器。我不想(由于其他原因)使用SDK和/或多个文件/库。我只需要一个函数- fileUpload -就是这样。
我能够得到的关键使用另一个函数没有任何问题,但当我试图上传文件使用下面的fileUpload函数我面临着以下错误
InvalidHeaderValue其中一个HTTP标头的值格式不正确。RequestId:798508 a1 - 701 e-0055- 0 e82-efab 9 f000000时间:2023-09- 25 T07:34:50. 5926245 ZContent-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;
}

你能帮帮我吗。提前感谢!

kokeuurv

kokeuurv1#

将视频上传到Azure blob存储的PHP cURL
要使用**Put blob**请求将视频从本地上传到Azure blob存储,您可以使用以下PHP代码。
我使用了一个5 MB的示例视频文件来上传Azure Blob存储。

验证码:

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

**控制台:**x1c 0d1x
输出:

入口:

参考:

Put Blob(REST API)- Azure存储|微软学习

相关问题