我有問題來創建一個新的/簡單的PHP腳本/函數與cURL上傳簡單文件(image.jpg)從本地服務器到Azure存儲 - 使用共享密鑰身份驗證的blob容器。我不希望(由於其他原因)使用SDK和/或多個文件/庫。我只需要一個函數 - fileUpload - 就是這樣。簡單的PHP cURL文件上傳到Azure存儲blob
文檔沒有提供用文件覆蓋上傳(POST)的完整示例。 https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/authentication-for-the-azure-storage-services
目前還不清楚什麼是必需的標題,以及對簽名/認證屬性需要哪些標題。 (多MSDN網站有不同的標題,例如 - https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/put-blob
是否有人對這個簡單的例子,就是想分享
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
date_default_timezone_set('UTC');
$destinationURL = 'https://mystorage.blob.core.windows.net/blob1/image.jpg';
$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',
'file_contents'=>'@'.$filetoUpload
);
$headerText=""
."x-ms-version: 2015-02-21\n"
."x-ms-date:" .$currentDate." GMT\n"
."Content-Type: text/plain; charset=UTF-8\n"
."x-ms-blob-content-disposition: attachment; filename=".$filetoUpload."\n"
."x-ms-blob-type: BlockBlob\n"
."x-ms-meta-m1: v1\n"
."x-ms-meta-m2: v2\n"
;
$hash = hash_hmac('sha256', $headerText, base64_decode($accesskey), true);
$sig = base64_encode($hash);
$headerText.="Authorization: SharedKey ".$storageAccount.":".$sig."\n";
$headerText.="Content-Length: 280";
$headers = explode("\n", $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/>');
print_r($result);
echo ('Error<br/>');
print_r(curl_error($ch));
curl_close ($ch);
return;
}
upload($filetoUpload, $storageAccount,$destinationURL,$accesskey);
?那麼,你到底嘗試了什麼? – evilSnobu
編輯問題並給出當前進度 –