2017-01-16 18:00
浏览 211

简单的PHP cURL文件上传到Azure存储blob

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

Documentation does not provide complete example for upload (POST) with file overwrite.

It is also unclear what are required headers to have, and what are required headers to have for signature / authentication attribute. (multiple MSDN sites with different headers, for instance -

Does someone has simple example on this that would like to share?

ini_set('display_errors', 'On');

$destinationURL = '';
$accesskey = "qJXTMmw2Esal8/tXgfWk0RLwMNJ6OQKEt0E8EMZ8VhQrzGi5uqJqeKvi1l7iqnOddp7bdtai5rPpC6ynHttl1w==";
$storageAccount = 'mystorage';
$filetoUpload = realpath('./image.jpg');

function upload($filetoUpload, $storageAccount,$destinationURL,$accesskey) {
    $currentDate = date("D, d M Y H:i:s");

    $postFields = array(
        'extra_info' => '123456',

        ."x-ms-version: 2015-02-21
        ."x-ms-date:" .$currentDate." GMT
        ."Content-Type: text/plain; charset=UTF-8
        ."x-ms-blob-content-disposition: attachment; filename=".$filetoUpload."
        ."x-ms-blob-type: BlockBlob
        ."x-ms-meta-m1: v1
        ."x-ms-meta-m2: v2

    $hash = hash_hmac('sha256', $headerText, base64_decode($accesskey), true);
    $sig = base64_encode($hash);

    $headerText.="Authorization: SharedKey ".$storageAccount.":".$sig."
    $headerText.="Content-Length: 280";
    $headers = explode("
", $headerText);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_URL, $destinationURL);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $result = curl_exec($ch);
    echo ('Result<br/>');
    echo ('Error<br/>');
    curl_close ($ch);

upload($filetoUpload, $storageAccount,$destinationURL,$accesskey);
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题