我正在使用rest API在Azure存储中创建容器。我想给它提供公共权限或容器权限,以便动态地将blob上传到容器中。我正在嘲笑" x-ms-blob-public-access:container"在标题中,但当我包括下面的错误
结果HTTP / 1.1 403服务器无法验证请求。使 确保正确形成Authorization标头的值,包括 签名。内容长度:709内容类型:application / xml 服务器:Microsoft-HTTPAPI / 2.0 x-ms-request-id: 832b00af-501e-0032-7c96-f1ba27000000日期:星期二,2018年5月22日06:29:51 GMT AuthenticationFailedServer无法验证请求。 确保正确形成Authorization标头的值 包括签名。 请求ID:832b00af-501E-0032-7c96-f1ba27000000 时间:2018-05-22T06:29:51.7678173Z在HTTP中找到的MAC签名 请求' bmJk1ntjZX17woKGTm1YjEaIU3N / NSOXt / a8Gq4lCI0 ='不一样 任何计算签名。服务器使用以下字符串进行签名:' PUT 0 x-ms-blob-public-access:container x-ms-date:Tue,2018年5月22日 06:29:51 GMT x-ms-version:2009-09-19 / dsmsstor1 / 66-62-108-images restype:容器' .Error
我想我必须把" x-ms-blob-public-access:container"在sig字符串中,但我无法正确完成。帮我形成正确的sig字符串。 ie(PUT \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n-ms-date:"。$ date。" \ nx-ms-version :" $版本" \ N / dsmsstor1 / 66-62-108图像\ nrestype:。容器) 我的代码 ----------
<?php
date_default_timezone_set ( 'GMT' );
$date = date ( "D, j M Y H:i:s T" );
$version = "2009-09-19";
$account_key = 'xxxxxxxxx';
$utf8_encode_str = utf8_encode ("PUT\n\n\n0\n\n\n\n\n\n\n\n\nx-ms-date:". $date . "\nx-ms-version:".$version."\n/dsmsstor1/66-62-108-images\nrestype:container" );
$signature_str = base64_encode(hash_hmac('sha256',$utf8_encode_str, base64_decode($account_key), true));
$header = array (
"x-ms-date: " . $date,
"x-ms-version: " . $version,
"Authorization: SharedKey dsmsstor1:" . $signature_str,
"Content-Length: 0",
"x-ms-blob-public-access:container"
);
$url="https://dsmsstor1.blob.core.windows.net/66-62-108-images?restype=container";
$ch = curl_init ();
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, 'PUT' );
curl_setopt ($ch, CURLOPT_URL, $url );
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt ($ch, CURLOPT_HEADER, True );
$result = curl_exec ( $ch );
echo ('Result<br/>');
print_r($result);
echo ('Error<br/>');
print_r(curl_error($ch));
curl_close($ch);
?>
答案 0 :(得分:0)
您x-ms-blob-public-access:container
中遗漏了$utf8_encode_str
。
请尝试更改以下代码行:
$utf8_encode_str = utf8_encode ("PUT\n\n\n0\n\n\n\n\n\n\n\n\nx-ms-date:". $date . "\nx-ms-version:".$version."\n/dsmsstor1/66-62-108-images\nrestype:container" );
到
$utf8_encode_str = utf8_encode ("PUT\n\n\n0\n\n\n\n\n\n\n\n\nx-ms-blob-public-access:container\nx-ms-date:". $date . "\nx-ms-version:".$version."\n/dsmsstor1/66-62-108-images\nrestype:container" );