【问题标题】:Angular $http put request to azure storageAngular $http 向天蓝色存储提出请求
【发布时间】:2017-12-15 21:01:57
【问题描述】:

我在尝试将图像上传到 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

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

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

【问题讨论】:

    标签: angularjs azure http-headers azure-storage


    【解决方案1】:

    授权头的格式如下:

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

    • 签名字符串不是您的访问密钥,它应该针对存储访问密钥、请求标头等生成。构造方法请参考this documentation

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

    【讨论】:

      【解决方案2】:

      好吧,我终于弄明白了。问题是我发布的 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;
          }
        };
      });}]);
      

      请考虑到,如果您这样做,您是针对从客户端发出的每个请求都执行此操作,因此您需要处理您的请求以仅针对特定请求进行设置。

      不需要授权,因为我的变量“urlToUpload”中的授权 url 已经采用正确的格式(要求 azure 给我授权的 url 来上传文件)。该类型是自动继承的,因此无需设置。

      感谢陈亚伦的回复!

      【讨论】:

        猜你喜欢
        • 2017-11-16
        • 2013-08-08
        • 1970-01-01
        • 2019-02-24
        • 2018-09-01
        • 2022-01-08
        • 2012-11-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多