英文:
Calling the Azure Storage REST API directly from PHP
问题
以下是翻译好的部分:
现在,Azure Storage PHP 客户端 已被弃用,Microsoft 建议 直接调用 Azure Storage REST API。唯一的问题是,我不知道如何通过 PHP 开始进行这个操作,基本上所有我通过 Google 和搜索 SO/SE 找到的文档都与使用上述 Composer 客户端库相关。
是否有人有一个工作示例,例如,如何通过 PHP 直接调用 API 来上传 PDF 附件到 Blob 存储容器?
至今,我尝试寻找与 PHP 在这个主题上相关的文档,但都没有找到。
英文:
Now that the Azure Storage PHP client is deprecated, Microsoft recommends calling the Azure Storage REST API directly. The only problem is, I have no idea where to start with doing this through PHP, and basically all the documentation I come across through Google and searching SO/SE is related to using the aforementioned composer client libraries.
Does anyone have a working example of how to, for example, upload a PDF attachment to a blob storage container by calling the API directly using PHP?
I've tried finding documentation specific to PHP on this subject and I have so far come up empty.
答案1
得分: 1
以下是翻译好的部分:
通过 PHP 直接调用 API 上传 PDF 附件到 Blob 存储容器?
您可以使用以下 PHP 代码来通过直接调用 API 上传 PDF 文件。
代码:
<?php $storageAccountname = 'your storage account name'; $accesskey = "your storage account access key"; $filepath = realpath('./sample1.pdf'); $containerName = 'test'; $blobName = 'demo.pdf'; $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[] = 'application/pdf'; /*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: application/pdf', '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);
输出:
门户:
英文:
> Upload a PDF attachment to a blob storage container by calling the API directly using PHP?
You can use the below PHP code to upload the pdf file by calling the API directly.
Code:
<?php
$storageAccountname = 'your storage account name';
$accesskey = "your storage account access key";
$filepath = realpath('./sample1.pdf');
$containerName = 'test';
$blobName = 'demo.pdf';
$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[] = 'application/pdf'; /*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: application/pdf',
'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);
Output:
Portal:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论