gpt4 book ai didi

php - 从 Azure 存储 blob 下载文件的简单 PHP cURL 方法

转载 作者:行者123 更新时间:2023-12-02 23:16:18 26 4
gpt4 key购买 nike

我正在使用简单的 PHP cURL 函数将文件从本地服务器上传到 Azure 存储 - blob 容器。我从下面的网址获取了代码。

https://stackoverflow.com/questions/41682393/simple-php-curl-file-upload-to-azure-storage-blob

有人可以告诉我任何可以从 Azure 存储 - blob 下载文件的链接或代码,无需任何 SDK 和/或多个文件/库。它应该像上传功能。

提前致谢!

最佳答案

这是sample我提到的。非常详细,可以帮助你理解使用cURL请求API,如Get Blob API .

请求:

GET https://myaccount.blob.core.windows.net/mycontainer/myblob
x-ms-date: Mon, 24 Apr 2021 06:34:09 GMT
x-ms-version: 2014-02-14
Authorization: SharedKey myaccount:<signature_str>

你可以引用这个。

<?php

$storageAccount = '';
$containerName = '';
$blobName = '';
$account_key = '';

$date = gmdate('D, d M Y H:i:s \G\M\T');
$version = "2019-12-12";

$stringtosign = "GET\n\n\n\n\n\n\n\n\n\n\n\nx-ms-date:". $date . "\nx-ms-version:".$version."\n/".$storageAccount."/".$containerName."/".$blobName;
$signature = 'SharedKey'.' '.$storageAccount.':'.base64_encode(hash_hmac('sha256', $stringtosign, base64_decode($account_key), true));
echo "\n\n" . $signature;

$header = array (
"x-ms-date: " . $date,
"x-ms-version: " . $version,
"Authorization: " . $signature
);

$url="https://$storageAccount.blob.core.windows.net/$containerName/$blobName";
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, 'GET' );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header);
curl_exec ( $ch );
$result = curl_exec($ch);
echo "\n\n" . $result;

if(curl_errno($ch)){
throw new Exception(curl_error($ch));
}

file_put_contents('D://demo//downloaded.txt', $result); // save the string to a file

curl_close($ch);

关于php - 从 Azure 存储 blob 下载文件的简单 PHP cURL 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64992256/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com