Angular $ http将请求放入azure存储

时间:2017-12-13 16:43:11

标签: angularjs azure http-headers azure-storage

尝试将图像上传到Azure存储帐户时遇到问题。问题在于使用配置标头将请求正确地发送到服务器。我已经测试了这个,邮递员自己做了请求,并且像魅力一样工作,但是当试图从角度使用$ http.put时,它给了我错误

  

400(验证信息的格式不正确。请检查授权标题的值。)

这是我的javascript函数

uploadToAzure: function (urlToUpload, fileType, file) {
    var formattedUrl = urlToUpload.split('"')[1];
    var fecha = new Date();
    var config = {
      headers:{}}
    config.headers = {
        'x-ms-blob-type': 'Blockblob',
        'x-ms-blob-content-type': fileType,
        'x-ms-date': fecha,
        'x-ms-version': '2017-04-17',
        //'Host': 'necesito1.blob.core.windows.net',
        'Authorization_Token': 'SharedKey <myAccount>:<MyPublicAccessKey>'
      };
    return $http.put(urlToUpload, file._file, config);

我已经阅读了这些文章,在那里我找到了如何正确配置我的标题 Doc1 //// Stackoverflow answer

如果有帮助,我会从邮递员那里得到这个标题

enter image description here

我在这里遗漏了什么吗?任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

Authorization标头的格式如下:

Authorization="[SharedKey|SharedKeyLite] <AccountName>:<Signature>"
  • 授权标题的名称应为Authorization,而不是Authorization_Token

  • 签名字符串不是您的访问密钥,应根据存储访问密钥,请求标头等生成。关于如何构建它,请参阅this documentation

另一种选择是使用Shared Access Signature (SAS)。请参阅Azure storage authorization failed or format is wrong

答案 1 :(得分:0)

好的,所以我终于搞清楚了。问题是我发布的javascript错误,不是配置,而是我设置标题的方式。 进一步挖掘我发现按照我的方式设置标题是不行的

return $http.put(urlToUpload, file._file, config);

我最终覆盖了$ httpProvider的拦截器服务。您需要做的是在app.js文件中覆盖此功能,如此

angular.module('yourAppName', ['yourInyectedModules']).config(['$httpProvider', function($httpProvider) {
$httpProvider.interceptors.push(function ($q) {
  return {
    'request': function (config) {
       //Do your custom processing here
        config.headers = {
          'x-ms-blob-type': 'Blockblob' 
        };
        return config;       
    },

    'response': function (response) {
      // same as above
      return response;
    }
  };
});}]);

考虑到如果你这样做,你就是为客户端提出的每一个请求而做的,所以你需要处理你的请求,只为特定的请求设置它。

我没有授权,因为我的变量中的授权网址格式已经正确,并且#34; urlToUpload&#34; (要求azure给我授权的URL来上传文件)。该类型是自动继承的,因此无需进行设置。

感谢你的答复Aaron Chen!