Azure Blob Service REST API - 错误: read ECONNRESET

3

目标及进展

我正在尝试使用Node.JS使用Azurue存储Blob服务REST API。到目前为止,我成功地完成了列出容器获取Blob服务属性操作的请求。现在,我正在尝试基于MS的此文档进行放置Blob

我遇到了400和403错误,并搜索了类似的问题,即使是关于C#或R而不是Node.JS的问题问题,阅读它也帮助我理解可能做错了什么并更改我的代码。在这种情况下,签名及其规范化资源和规范化标头缺乏更清晰的文档。

问题

当我认为我解决了签名问题(因为所有的响应都没有告诉我那是问题),现在我不再有这些错误,所有发生的事情就是:我发送请求后它会冻结一段时间;一段时间后我会收到消息:
events.js:292
      throw er; // Unhandled 'error' event
      ^

Error: read ECONNRESET
    at TLSWrap.onStreamRead (internal/stream_base_commons.js:205:27)
Emitted 'error' event on ClientRequest instance at:
    at TLSSocket.socketErrorListener (_http_client.js:426:9)
    at TLSSocket.emit (events.js:315:20)
    at emitErrorNT (internal/streams/destroy.js:92:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
    at processTicksAndRejections (internal/process/task_queues.js:84:21) {
  errno: 'ECONNRESET',
  code: 'ECONNRESET',
  syscall: 'read'
}

类似问题的这个问题中,我了解到它是TCP另一端的连接中断。所以很可能不再是签名的问题,但我不知道是什么问题。在Put Blob之前,我需要进行另一种类型的请求吗?

我尝试更改签名,还尝试创建要上传的本地.txt文件,并尝试从变量上传简单字符串。我不确定数据应该从哪里来进行上传。

我认为我的主要问题是,对于其他错误和问题,我得到了一些信息(最终在阅读了很多资料后)来解决它;但现在我甚至没有得到状态200。

代码

我的创建签名的函数:

/**
   * Authorization using HMAC SHA 256.
   * @param {String} VERB - Request method to be used (GET, PUT).
   * @param {String} strTime - Time of the request, in RFC 1123 Format.
   * @param {String} path - Path of the URL, containing the name of the
   * container and the query parameters.
   */
  create_signature(VERB, strTime, uri, content) {
    VERB = VERB.toUpperCase();

    // removing first slash
    uri = uri.replace("/","");
    
    // separating '/container/blob?q=query&q=query' into 'container/blob' and 'q=query&q=query'
    var [path, query] = uri.split("?");
    // changing 'q=query&q=query' to 'q:query\nq:query' if '?' is included
    query = query ? query.replace(/\=/g,":").replace(/\&/g,"\n") : '';
    // without the '?' char the separation is '/container/blob' and ''
    
    const content_type = "text/plain; charset=UTF-8";
    const content_length = content.length.toString();
    
    let strToSign = VERB + "\n" + // VERB
      "\n" +                      // Content-Encoding
      "\n" +                      // Content-Language
      content_length + "\n" +     // Content-Length
      "\n" +                      // Content-MD5
      content_type + "\n" +         // Content-Type
      "\n" +                      // Date
      "\n" +                      // If-Modified-Since
      "\n" +                      // If-Match
      "\n" +                      // If-None-Match
      "\n" +                      // If-Unmodified-Since
      "\n" +                      // Range
      // CanonicalizedHeaders
      `x-ms-blob-type:BlockBlob` + "\n" +
      `x-ms-date:${strTime}` + "\n" + 
      `x-ms-version:${this.version}` + "\n" +
      // CanonicalizedResource
      `/${this.account_name}/${path}`;

    console.log(strToSign);
    // strToSign = strToSign.toLowerCase();
    // strToSign = encodeURIComponent(strToSign);

    // generating secret from account key
    var secret = CryptoJS.enc.Base64.parse(this.account_key);
    // encrypting the signature
    var hash = CryptoJS.HmacSHA256(strToSign, secret);
    var hashInBase64 = CryptoJS.enc.Base64.stringify(hash);
    var auth_sig = `SharedKey ${this.account_name}:` + hashInBase64;

    return auth_sig;
  }

使用http模块进行请求:
// making the request using the http module:
  put_blob(container_name, filename) {
    const time_UTC_str = new Date().toUTCString();
    var path = `/${container_name}/${filename}`;

    const obj = "hello world";

    const signature = this.create_signature('PUT', time_UTC_str, path, obj);


    const req_params = {
      method: 'PUT',
      hostname: this.hostname,
      path: path,
      headers: {
        'Authorization': signature,
        'x-ms-date': time_UTC_str,
        'x-ms-version': this.version,
        'x-ms-blob-type': 'BlockBlob',
        'Content-Length': obj.length.toString(),
        'Content-Type': "text/plain; charset=UTF-8"
      }
    }

    let req = http.request(req_params, this.res_handler);
    req.end();
  }

响应处理程序函数:
  /**
 * Callback function that handles and parses the responses, 
 * including how the search results will be processed.
 * @param {object} res - response from request
 */
  res_handler = function (res) {

    let body = '';
    // storing body
    res.on('data', (dat) => {
      body += dat;
    });

    // when signaling 'end' flag
    res.on('end', () => {
      // parsing response
      if (!res.complete) {
        console.error('The connection was terminated while the message was still being sent');
      } else {
        // console.log(res);
        console.log(`Status: ${res.statusCode} - ${res.statusMessage}`);
      }
      console.log('Response: ' + body);
      
    });

    // handling errors
    res.on('error', (err) => {
      console.error(`Error ${err.statusCode}: ${err.statusMessage}`);
    });

  };

PS:我应该尝试另一个云服务,还是它们都很复杂且文档不好?

1个回答

3

我忘记使用req.write()来实际写入数据。我很尴尬,但我将解释发生了什么以防其他人有类似的问题。

如我之前所解释的,该错误源于来自另一侧的连接中断,例如由于闲置超时。因此,它一直等待我向请求写入数据,因为我没有这样做,最终停止等待,从而返回该错误。

事实证明这与Azure或Blob服务无关,我只是没有发送任何要上传的内容。在这个过程中,花费了我一些时间才意识到这一点,但我通过研究这个问题学到了很多。

put_blob()方法的最后几行现在看起来像这样:

let req = http.request(req_params, this.res_handler);
req.write(obj);    // it was missing this! 
req.end();

现在我(希望)永远不会忘记在PUT请求中写入数据了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接