- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将 Azurue 存储 Blob 服务 REST API 与 Node.JS 结合使用。到目前为止,我成功地请求了 List Containers
和 Get Blob Services Properties
操作。现在我正在尝试基于 this documentation 的 Put Blob
来自微软。
我遇到过 400 和 403 错误,并搜索过类似的问题,即使它是 a question on C#或on R阅读它而不是 Node.JS,可以帮助我了解我可能做错了什么并更改我的代码。在这种情况下,签名及其规范化资源和规范化 header 缺乏更清晰的文档。
当我以为我解决了签名问题(毕竟响应不再告诉我这就是问题所在)并且现在我不再有这些错误时,所发生的一切是:我发出请求,它卡住了一段时间尽管;一段时间后我收到消息:
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'
}
来自this question on a similar issue我了解到这是 TCP 另一端的连接中断。所以它可能不再是签名了,但我不知道它是什么。我是否需要在 Put Blob
之前发出其他类型的请求?
我尝试更改签名,还尝试创建要上传的本地 .txt 文件,并尝试从变量上传简单的字符串。我不确定数据应该从哪里上传。
我认为我的主要问题是,对于我遇到的其他错误和问题,我得到了一些信息来(最终在大量阅读之后)解决它;但现在我什至不再获得 Status 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: Should I try another cloud service or are they all complicated and bad documented?
最佳答案
我忘记使用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 请求上写入数据。
关于node.js - Azure Blob 服务 REST API - 错误 : read ECONNRESET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66171580/
尝试从终端运行命令 heroku run python manage.py migrate 时,出现以下错误。 ECONNRESET:读取ECONNRESET 我关注了the link在heroku
我开发了一个 Firebase Cloud 函数,可以处理对上传图像的多种操作。 我的代码基于 this documentation article和 this Cloud Function exam
我尝试使用 Volley 库并将图像上传到服务器。该库应该在独立模式下执行此过程,但收到以下错误消息: java.net.SocketException: sendto failed: ECONNRE
我的程序使用字符串数组解析带有HTTPS代理的文本文档。然后,它向ipify.org发出GET请求。 但是,我的程序抛出了:Error: read ECONNRESET at TCP.onStream
我尝试通过 npm 安装 Electron 并收到此错误: 怎么解决呢? 最佳答案 您可以设置自定义镜像以从单独的镜像下载 Electron 。只需在 npm install 之前添加此内容,或在某个
我正在尝试将我的 Nodejs 应用程序连接到 MongoDB Atlas,但它抛出错误。 这是错误消息: This is the error in the catch block: Mongoose
我有一个在 Azure Web 应用程序上托管的 ExpressJS 应用程序,并启用了“始终在线”功能。 我正在使用 bookshelfjs 连接到托管在 Azure 上另一个虚拟服务器中的 MyS
这是我的下载代码: File file = new File(dir, fileName); HttpClient httpclient = new D
我在应用程序中使用 Express.js,但在向 adobe Analytics API 发出发布请求时收到错误。 我尝试添加 server.timeout 但它无法修复它...... 这是错误消息:
我在执行 GET 请求时遇到错误。 Error: read ECONNRESET at TLSWrap.onStreamRead (internal/stream_base_commons.j
我们有基于 Cucumber 的 UI 自动化测试框架。最近我们已经从 Ruby 1.9.x 迁移到 2.2.0,之后我们在通过测试框架登录我们的应用程序时遇到了问题。它说 SSL 连接重置问题。 错
以下代码有时会生成“连接被对等方重置”错误。谁能告诉我如何处理这个异常? doc = Nokogiri::HTML(open(url)) Connection reset by peer (Errno
我的很多 node.js 进程都因 ECONNRESET 错误而崩溃。这是我可以看到的输出: node.js:50 throw e; ^ Error: ECONNRESET, Conn
我是编码的新手,我需要一些帮助来解决我遇到的一个错误。 我正在运行一个 NodeJS 应用程序(一个 Discord.js 机器人),我的机器人具有在用户键入特定命令时将用户 ID 注册到 MySQL
我的客户端 (PHP) 反复向 Node.js net.Server 发送消息,并收到意外错误。 服务器部分: socket.on('error',function(err){ logger.
我知道对于 TCP 套接字 ECONNRESET 与 RST 数据包有关。但是我在 read() 和 write() 调用上也看到了 AF_LOCAL 套接字的 ECONNRESET 错误。这是什么意
我已经配置了一个fluentd sidecar容器来将日志发送到kafka。 这是我的fluent.conf文件: @type forward bind 127.0.0.1 port 2
我在 Azure 上的 Application Insight 中遇到此错误: Microsoft.AspNet.Server.Kestrel.Networking.UvException: Erro
我制作了很多机器人,一些在我的个人笔记本电脑上,一些在 Heroku 上,但是在这两种情况下,我都收到了终止 node.js 的错误,所以我使用了 bot.on('error ', console.e
我正在使用 Rails 应用程序和 MiniFB Gem 来调用 Facebook Api。我得到以下异常 Errno::ECONNRESET: Connection reset by peer 以
我是一名优秀的程序员,十分优秀!