简单 PHP cURL 文件上传到 Azure 存储 blob
Simple PHP cURL file upload to Azure storage blob
我在使用 cURL 创建 new/simple PHP script/function 以将简单文件 (image.jpg) 从本地服务器上传到 Azure 存储时遇到问题 - 使用共享的 blob 容器密钥认证。我不想(出于其他原因)使用 SDK and/or 多个 files/libraries。我只需要一个功能 - fileUpload - 就是这样。
文档未提供带有文件覆盖的上传 (POST) 的完整示例。
https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/authentication-for-the-azure-storage-services
还不清楚 headers 需要什么,签名/身份验证属性需要 headers 什么。 (具有不同 headers 的多个 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);
您可以试试下面的代码片段。
<?php
$accesskey = "qJXTMmw2Esal8/tXgfWk0RLwMNJ6OQKEt0E8EMZ8VhQrzGi5uqJqeKvi1l7iqnOddp7bdtai5rPpC6ynHttl1w==";
$storageAccount = 'mystorage';
$filetoUpload = realpath('./image.jpg');
$containerName = '<yourblobcontainer>';
$blobName = 'image.jpg';
$destinationURL = "https://$storageAccount.blob.core.windows.net/$containerName/$blobName";
function uploadBlob($filetoUpload, $storageAccount, $containerName, $blobName, $destinationURL, $accesskey) {
$currentDate = gmdate("D, d M Y H:i:s T", time());
$handle = fopen($filetoUpload, "r");
$fileLen = filesize($filetoUpload);
$headerResource = "x-ms-blob-cache-control:max-age=3600\nx-ms-blob-type:BlockBlob\nx-ms-date:$currentDate\nx-ms-version:2015-12-11";
$urlResource = "/$storageAccount/$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[] = 'image/png'; /*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 $storageAccount:$sig";
$headers = [
'Authorization: ' . $authHeader,
'x-ms-blob-cache-control: max-age=3600',
'x-ms-blob-type: BlockBlob',
'x-ms-date: ' . $currentDate,
'x-ms-version: 2015-12-11',
'Content-Type: image/png',
'Content-Length: ' . $fileLen
];
$ch = curl_init($destinationURL);
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 ('Result<br/>');
print_r($result);
echo ('Error<br/>');
print_r(curl_error($ch));
curl_close($ch);
}
uploadBlob($filetoUpload, $storageAccount, $containerName, $blobName, $destinationURL, $accesskey);
我在使用 cURL 创建 new/simple PHP script/function 以将简单文件 (image.jpg) 从本地服务器上传到 Azure 存储时遇到问题 - 使用共享的 blob 容器密钥认证。我不想(出于其他原因)使用 SDK and/or 多个 files/libraries。我只需要一个功能 - fileUpload - 就是这样。
文档未提供带有文件覆盖的上传 (POST) 的完整示例。 https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/authentication-for-the-azure-storage-services
还不清楚 headers 需要什么,签名/身份验证属性需要 headers 什么。 (具有不同 headers 的多个 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);
您可以试试下面的代码片段。
<?php
$accesskey = "qJXTMmw2Esal8/tXgfWk0RLwMNJ6OQKEt0E8EMZ8VhQrzGi5uqJqeKvi1l7iqnOddp7bdtai5rPpC6ynHttl1w==";
$storageAccount = 'mystorage';
$filetoUpload = realpath('./image.jpg');
$containerName = '<yourblobcontainer>';
$blobName = 'image.jpg';
$destinationURL = "https://$storageAccount.blob.core.windows.net/$containerName/$blobName";
function uploadBlob($filetoUpload, $storageAccount, $containerName, $blobName, $destinationURL, $accesskey) {
$currentDate = gmdate("D, d M Y H:i:s T", time());
$handle = fopen($filetoUpload, "r");
$fileLen = filesize($filetoUpload);
$headerResource = "x-ms-blob-cache-control:max-age=3600\nx-ms-blob-type:BlockBlob\nx-ms-date:$currentDate\nx-ms-version:2015-12-11";
$urlResource = "/$storageAccount/$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[] = 'image/png'; /*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 $storageAccount:$sig";
$headers = [
'Authorization: ' . $authHeader,
'x-ms-blob-cache-control: max-age=3600',
'x-ms-blob-type: BlockBlob',
'x-ms-date: ' . $currentDate,
'x-ms-version: 2015-12-11',
'Content-Type: image/png',
'Content-Length: ' . $fileLen
];
$ch = curl_init($destinationURL);
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 ('Result<br/>');
print_r($result);
echo ('Error<br/>');
print_r(curl_error($ch));
curl_close($ch);
}
uploadBlob($filetoUpload, $storageAccount, $containerName, $blobName, $destinationURL, $accesskey);