从PHP直接调用Azure Storage REST API

huangapple go评论81阅读模式
英文:

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

输出:
从PHP直接调用Azure Storage REST API

门户:
从PHP直接调用Azure Storage REST API

英文:

> 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:

&lt;?php
$storageAccountname = &#39;your storage account name&#39;;
$accesskey = &quot;your storage account access key&quot;;
$filepath = realpath(&#39;./sample1.pdf&#39;);
$containerName = &#39;test&#39;;
$blobName = &#39;demo.pdf&#39;;
$URL = &quot;https://$storageAccountname.blob.core.windows.net/$containerName/$blobName&quot;;
function uploadBlob($filepath, $storageAccountname, $containerName, $blobName, $URL, $accesskey) {
$Date = gmdate(&#39;D, d M Y H:i:s \G\M\T&#39;);
$handle = fopen($filepath, &quot;r&quot;);
$fileLen = filesize($filepath);
$headerResource = &quot;x-ms-blob-cache-control:max-age=3600\nx-ms-blob-type:BlockBlob\nx-ms-date:$Date\nx-ms-version:2019-12-12&quot;;
$urlResource = &quot;/$storageAccountname/$containerName/$blobName&quot;;
$arraysign = array();
$arraysign[] = &#39;PUT&#39;;               /*HTTP Verb*/  
$arraysign[] = &#39;&#39;;                  /*Content-Encoding*/  
$arraysign[] = &#39;&#39;;                  /*Content-Language*/  
$arraysign[] = $fileLen;            /*Content-Length (include value when zero)*/  
$arraysign[] = &#39;&#39;;                  /*Content-MD5*/  
$arraysign[] = &#39;application/pdf&#39;;   /*Content-Type*/  
$arraysign[] = &#39;&#39;;                  /*Date*/  
$arraysign[] = &#39;&#39;;                  /*If-Modified-Since */  
$arraysign[] = &#39;&#39;;                  /*If-Match*/  
$arraysign[] = &#39;&#39;;                  /*If-None-Match*/  
$arraysign[] = &#39;&#39;;                  /*If-Unmodified-Since*/  
$arraysign[] = &#39;&#39;;                  /*Range*/  
$arraysign[] = $headerResource;     /*CanonicalizedHeaders*/
$arraysign[] = $urlResource;        /*CanonicalizedResource*/
$str2sign = implode(&quot;\n&quot;, $arraysign);
$sig = base64_encode(hash_hmac(&#39;sha256&#39;, urldecode(utf8_encode($str2sign)), base64_decode($accesskey), true));  
$authHeader = &quot;SharedKey $storageAccountname:$sig&quot;;
$headers = [
&#39;Authorization: &#39; . $authHeader,
&#39;x-ms-blob-cache-control: max-age=3600&#39;,
&#39;x-ms-blob-type: BlockBlob&#39;,
&#39;x-ms-date: &#39; . $Date,
&#39;x-ms-version: 2019-12-12&#39;,
&#39;Content-Type: application/pdf&#39;,
&#39;Content-Length: &#39; . $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, &quot;PUT&quot;);
curl_setopt($ch, CURLOPT_INFILE, $handle); 
curl_setopt($ch, CURLOPT_INFILESIZE, $fileLen); 
curl_setopt($ch, CURLOPT_UPLOAD, true); 
$result = curl_exec($ch);
echo (&#39;Uploaded successfully&lt;br/&gt;&#39;);
print_r($result);
curl_close($ch);
}
uploadBlob($filepath, $storageAccountname, $containerName, $blobName, $URL, $accesskey);

Output:
从PHP直接调用Azure Storage REST API

Portal:
从PHP直接调用Azure Storage REST API

huangapple
  • 本文由 发表于 2023年6月6日 01:15:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76408667.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定