且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

授权Azure存储服务REST API

更新时间:2022-06-20 10:10:15

Chrome开发者工具刚刚没有返回"Access-Control-Allow-Origin"标头

Chrome Developer Tool just returned No 'Access-Control-Allow-Origin' header

当您使用javascript直接从客户端向Azure存储服务器发送http请求时.这是常见的 CORS 问题.

As you are using javascript to send http requests against to Azure Storage Server directly from client. It's a common CORS issue.

发生此问题时,可以启用CORS,您的存储服务.简单来说,您可以利用 Microsoft Azure存储资源管理器来配置存储.

When you occur this issue, you can enable the CORS for your storage services. For simple, you can leverage Microsoft Azure Storage Explorer to configure your storage.

AuthenticationFailed

AuthenticationFailed

我在您的测试项目中根据您的代码段进行了两次修改,以使其正常工作.

I did two modifications based on your code snippet, in my test project to make it work.

  1. var hash = CryptoJS.HmacSHA256(strToSign, key);第二个参数应该是帐户密钥的base64解码,请参考
  1. var hash = CryptoJS.HmacSHA256(strToSign, key); The second parameter, should be a base64 decode from the account key, refer to the Azure Storage SDK for node.js
  2. In my test, I had to use SharedKey scheme and authenticate with SharedKey token to make your scenario to work.

这是我的测试代码段,仅供参考.

Here is my test code snippet, FYI.,

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; 

此外,出于安全原因,请在后端Web服务器中实施Azure存储服务.与在客户端上使用纯JavaScript一样,会将您的帐户名和帐户密钥公开,这将增加您的数据和信息的风险.

Additionally, for security reason, please implement the Azure Storage Services in backend web server. As using pure javascript on client, will expose your account name and account key to public, which will raise the risk to your data and info.