Ern*_*nie 0 javascript azure-storage
我第一次调用 Azure 存储 REST API 时,一整天都被困住了。Postman 的回复显示是 Azure 身份验证出错,但我不知道是什么问题。
这是发送 Azure 存储 REST API 的浏览器脚本:
function azureListContainers() {
var key = "key-copied-from-azure-storage-account";
var strTime = (new Date()).toUTCString();
var strToSign = 'GET\n\n\n\nx-ms-date:' + strTime + '\nx-ms-version:2015-12-11\n/myaccount/?comp=list';
var hash = CryptoJS.HmacSHA256(strToSign, key);
var hashInBase64 = CryptoJS.enc.Base64.stringify(hash);
var auth = "SharedKeyLite myaccount:"+hashInBase64;
console.log(strToSign);
console.log(auth);
console.log(strTime);
$.ajax({
type: "GET",
beforeSend: function (request)
{
request.setRequestHeader("Authorization", auth);
request.setRequestHeader("x-ms-date", strTime);
request.setRequestHeader("x-ms-version", "2015-12-11");
},
url: "https://myaccount.blob.core.windows.net/?comp=list",
processData: false,
success: function(msg) {
console.log(msg);
}
});
}
Run Code Online (Sandbox Code Playgroud)
Chrome 开发人员工具刚刚返回 No 'Access-Control-Allow-Origin' 标头,没有进一步的原因,所以我复制了var auth和的内容,var strTime使用邮递员工具创建了相同的请求:
[Command]
GET https://myaccount.blob.core.windows.net/?comp=list
[Headers]
Authorization:SharedKeyLite myaccount:Z9/kY/D+osJHHz3is+8yJRqhj09VUlr5n+PlePUa8Lk=
x-ms-date:Tue, 09 Aug 2016 10:30:49 GMT
x-ms-version:2015-12-11
[Response Body]
<?xml version="1.0" encoding="utf-8"?>
<Error>
<Code>AuthenticationFailed</Code>
<Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
RequestId:9be3d595-0001-0012-4929-f2fde2000000
Time:2016-08-09T10:31:52.6542965Z</Message>
<AuthenticationErrorDetail>The MAC signature found in the HTTP request 'Z9/kY/D+osJHHz3is+8yJRqhj09VUlr5n+PlePUa8Lk=' is not the same as any computed signature. Server used following string to sign: 'GET
x-ms-date:Tue, 09 Aug 2016 10:30:49 GMT
x-ms-version:2015-12-11
/myaccount/?comp=list'.</AuthenticationErrorDetail>
</Error>
Run Code Online (Sandbox Code Playgroud)
在区分两个字符串之后,我相信var strToSign我的脚本与 Azure 用来签名的字符串相同。但仍然存在身份验证错误。请帮助指出是什么问题。
Chrome 开发人员工具刚刚返回 No 'Access-Control-Allow-Origin' 标头
当您使用 javascript 直接从客户端向 Azure 存储服务器发送 http 请求时。这是一个常见的CORS问题。
发生此问题时,您可以为您的存储服务启用 CORS。简单来说,您可以利用Microsoft Azure 存储资源管理器来配置您的存储。
认证失败
我根据您的代码片段在我的测试项目中进行了两次修改以使其正常工作。
var hash = CryptoJS.HmacSHA256(strToSign, key);第二个参数,应该是来自账户密钥的 base64 解码,参考Azure Storage SDK for node.jsSharedKey方案并使用SharedKey令牌进行身份验证才能使您的场景正常工作。这是我的测试代码片段,仅供参考,
var key = "key-copied-from-azure-storage-account";
var strTime = (new Date()).toUTCString();
var strToSign = 'GET\n\n\n\n\n\n\n\n\n\n\n\nx-ms-date:' + strTime + '\nx-ms-version:2015-12-11\n/<accountname>/\ncomp:list';
var secret = CryptoJS.enc.Base64.parse(key);
var hash = CryptoJS.HmacSHA256(strToSign, secret);
var hashInBase64 = CryptoJS.enc.Base64.stringify(hash);
var auth = "SharedKey <accountname>:"+hashInBase64;
Run Code Online (Sandbox Code Playgroud)
此外,出于安全原因,请在后端 Web 服务器中实现 Azure 存储服务。由于在客户端使用纯 javascript,会将您的帐户名称和帐户密钥公开,这将增加您的数据和信息的风险。
| 归档时间: |
|
| 查看次数: |
4823 次 |
| 最近记录: |