java.net.SocketInputStream.read(未知源)中的连接重置 - Node.js - Azure文件服务

时间:2015-10-27 07:09:41

标签: node.js azure azure-storage azure-mobile-services node.js-stream

我们尝试使用Azure中的node.js创建服务以进行下载。我们正在发送writeStream作为响应。

更新:

goods_id

下载小于4MB的文件时工作正常。但是下载超过4MB的时候会给出错误。

var option = new Object();
option.disableContentMD5Validation = true;
option.maximumExecutionTimeInMs = 20 * 60000;
fileService.getFileToStream(shareName, dirPath, fileName, response, option, function (error, result, response) {
    if(!error) {
        if(response.isSuccessful) {
      console.log("Success!");
  }
    }
});

以下是示例java客户端代码。

java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at sun.security.ssl.InputRecord.readFully(Unknown Source)
at sun.security.ssl.InputRecord.read(Unknown Source)
at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
at sun.security.ssl.SSLSocketImpl.readDataRecord(Unknown Source)
at sun.security.ssl.AppInputStream.read(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read1(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at sun.net.www.MeteredStream.read(Unknown Source)
at java.io.FilterInputStream.read(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(Unknown Source)
at FileserviceTest.sendGET(FileserviceTest.java:58)
at FileserviceTest.main(FileserviceTest.java:18)

我们可以将写入流转换为缓冲流吗?如果是这样,我们如何发送回应。或者是否有任何其他方式来发送大量数据作为响应。请帮帮我。我是node.js的新手。

1 个答案:

答案 0 :(得分:3)

要从Azure文件存储中获取大于4MB的文件,请求标头x-ms-range-get-content-md5会导致状态代码400(错误请求)错误,请参阅{{1} } Azure文件存储的REST API文档https://msdn.microsoft.com/en-us/library/azure/dn194274.aspx,见下文:

enter image description here

所以我查看了Azure文件存储SDK for Node(https://github.com/Azure/azure-storage-node/blob/master/lib/services/file/fileservice.js)的来源。对于函数Get FilegetFileToTextgetFileToLocalFilecreateReadStream,您需要设置getFileToStream属性以避免错误,请参阅下文。

  
      
  • @param {boolean} [options.disableContentMD5Validation]设置为true时,下载文件时将禁用MD5验证。
  •   

并以options.disableContentMD5Validation的来源为例:

enter image description here

在NodeJS代码中,您需要在调用函数getFileToStream的前面添加代码response.disableContentMD5Validation = true;

  

更新代码

getFileToStream
  

2015-10-29更新:对于Java代码:

Java异常问题与移动服务代码无关。这是因为缺少名为// this sentence must be removed in Azure Mobile Service, it cause the error. // response.setHeader('Content-Type', 'application/json'); // add the argument {disableContentMD5Validation: true} fileService.getFileToStream(shareName, dirPath, fileName, response, {disableContentMD5Validation: true}, function (error, result, response) { if(!error) { if(response.isSuccessful) { console.log("Success!"); } } }); 的请求标头属性,如下所示。

X-ZUMO-APPLICATION

您可以在Azure门户的移动服务仪表板底部找到密钥con.addRequestProperty("X-ZUMO-APPLICATION", "<Manage Access Key>"); ,如下图所示:

enter image description here

点击<Manage Access Key>按钮,您可以看到如下两个键:

enter image description here

将任何一个添加到代码中,然后代码工作正常。

NodeJS for Azure移动服务中的服务器示例代码:

MANAGE KEYS

Java中的客户端示例代码:

exports.get = function(request, response) {
    var azure = require('azure-storage');
    var fileService = azure.createFileService('<storage_account_name>','<storage_access_key>');
    fileService.getFileToStream('taskshare', 'taskdirectory', 'taskfile', response,{disableContentMD5Validation:true}, function(error, result, res) {
                if(!error) {
                  console.log(result);
                  console.log(res);
                }
    });
};

请检查Azure门户中api的权限,请参阅下图:

enter image description here

最诚挚的问候。