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

使用Azure存储表API调用MERGE时遭遇AuthenticationFailed错误

解决Azure表服务MERGE请求AuthenticationFailed问题

核心问题分析及修复步骤

  1. CanonicalizedResource格式错误
    你当前的$target['canonical']设置为"/[STORAGE_ACCOUNT]/[STORAGE_TABLE]()",但针对单个实体的MERGE操作,CanonicalizedResource必须包含实体的PartitionKey和RowKey信息,正确格式应为:
"/[STORAGE_ACCOUNT]/[STORAGE_TABLE](PartitionKey='[PARTITION_KEY]',RowKey='1')"

Azure表服务的签名校验会严格匹配资源路径,GET/POST如果是针对表级操作(比如查询、插入),格式可能没问题,但MERGE是针对单个实体的更新,必须在CanonicalizedResource里明确实体的唯一标识,否则签名校验会失败。

  1. Content-MD5的补充校验
    虽然POST请求未传Content-MD5也能成功,但MERGE操作对请求体的校验更严格。建议生成请求体的MD5哈希并填入签名数组的Content-MD5字段,确保签名包含完整的请求元数据。生成Content-MD5的代码示例:
$requestBody = '{"你的字段": "更新后的值"}'; // 替换为实际MERGE请求体
$contentMd5 = base64_encode(md5($requestBody, true));
  1. 请求头与签名参数的一致性检查
  • 确保MERGE请求的Content-Type头和签名里的$contentType完全一致,包括odata=nometadata参数,不能有大小写或空格差异。
  • 确认请求的Date头和签名生成时的$currentDate完全相同,必须是GMT格式的标准字符串(比如Wed, 10 Jul 2024 12:34:56 GMT)。

修正后的完整代码示例

$method = 'MERGE';
$currentDate = gmdate("D, d M Y H:i:s T");
$contentType = 'application/json;odata=nometadata';
// 替换为实际的MERGE请求体内容
$requestBody = '{"YourField": "UpdatedValue"}';
$contentMd5 = base64_encode(md5($requestBody, true));

$target = array(
  "href" => "https://[STORAGE_ACCOUNT].table.core.windows.net/[STORAGE_TABLE](PartitionKey='[PARTITION_KEY]',RowKey='1')",
  "canonical" => "/[STORAGE_ACCOUNT]/[STORAGE_TABLE](PartitionKey='[PARTITION_KEY]',RowKey='1')"
);

// 生成签名所需数组
$signatureArray = array(
  $method,               /*HTTP Verb*/
  $contentMd5,           /*Content-MD5*/
  $contentType,          /*Content-Type*/
  $currentDate,          /*Date*/
  $target['canonical']   /*CanonicalizedResource*/
);

// 生成签名
$signature = base64_encode(hash_hmac('sha256', urldecode(utf8_encode(implode("\n", $signatureArray))), base64_decode(variable_get('az_storage_key', '')), true));

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 22:43:18