You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PHP/REST API访问Azure Files时认证失败求助

Azure Files 自主代码读取文本文件认证失败问题

原本通过外部库可正常访问Azure Files,现改用自主编写的简洁代码读取文本文件时,遭遇认证失败错误:

AuthenticationFailed 服务器未能验证请求。请确保Authorization标头的值(包括签名)格式正确。RequestId:333b3e7c-401a-0076-2031-b36e2a000000 Time:2024-05-31T08:04:47.3863148Z
HTTP请求中的MAC签名'sdfoisiWAIPUnjOHSoiOSDPWhdsojfs=='与任何计算出的签名都不匹配。服务器用于签名的字符串为:'GET x-ms-date:Fri, 31 May 2024 08:04:47 GMT x-ms-file-range:bytes=0- x-ms-version:2019-02-02 /xxxx/file.txt'。

使用PHP 8.3.7,尝试多种签名组合仍未解决问题,寻求经验人士帮助。

以下是使用的代码:

<?php

$storageAccount = 'xxxxx';
$containerName = 'xxxxxx';
$fileName = 'xxx/xxxx/file.txt';
$account_key = 'xxxxxxxxxxxxxxxxxxxxxxxxx';

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

$stringtosign = "GET











x-ms-date:". $date . "
x-ms-version:".$version."
/".$storageAccount."/".$containerName."/".$fileName;
$signature = 'SharedKey'.' '.$storageAccount.':'.base64_encode(hash_hmac('sha256', $stringtosign, base64_decode($account_key), true));
echo "

" . $signature;

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


$url = 'https://' . $storageAccount . '.file.core.windows.net/' . $containerName . '/' . $fileName;


$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 "

" . $result;

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

curl_close($ch);
?>

内容的提问来源于stack exchange,提问作者Simius

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 03:27:22