2

我正在使用 node.js 为 Azure 文件存储创建文件服务。我正在使用azure-storage-node (http://azure.github.io/azure-storage-node/)这个。

我正在尝试从 Azure 文件存储下载文件。下面是我的代码片段

// Download a file from Share
exports.get = function(request, response){
    var shareName = request.headers.sharename;
    var dirPath = request.headers.directorypath;
    var fileName = request.headers.filename;

    var fileService = azure.createFileService();

    var readStream = fileService.createReadStream(shareName, dirPath, fileName);

    var dataLength = 0;
    var body = '';
    readStream.on('data', function (chunk) {
    dataLength += chunk.length;
  })

    readStream.on('end', function(){
      console.log('The length was:', dataLength);
    });

    response.setHeader('Content-Type', 'application/json');
    response.send(statusCodes.OK, JSON.stringify("Success!"));

}

我能够获得数据流。但是我怎样才能在响应中发送流,以便我们可以在 rest 调用中获取它。

我试过readStream.pipe(response);

response.write(typeof chunk);
response.end() but it doesnt work; 

我是 node.js 的新手。请帮助我。

更新:

我尝试了以下。

response.writeHead(200, {'Content-Type': 'application/json'});

var readStream = fileService.createReadStream(shareName, dirPath, fileName);
readStream.pipe(response);

但它抛出以下错误。

    ERROR
An unhandled exception occurred. Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (http.js:679:11)
    at ServerResponse.res.setHeader (D:\home\site\wwwroot\node_modules\express\node_modules\connect\lib\patch.js:59:22)
    at ServerResponse.res.set.res.header (D:\home\site\wwwroot\node_modules\express\lib\response.js:518:10)
    at addDefaultHeaders (D:\home\site\wwwroot\runtime\request\requesthandler.js:582:9)
    at ServerResponse.<anonymous> (D:\home\site\wwwroot\runtime\request\requesthandler.js:291:13)
    at ServerResponse._.wrap [as end] (D:\home\site\wwwroot\node_modules\underscore\underscore.js:692:22)
    at ChunkStream.onend (stream.js:66:10)
    at ChunkStream.EventEmitter.emit (events.js:126:20)
    at ChunkStream.end (D:\home\site\wwwroot\App_Data\config\scripts\node_modules\azure-storage\lib\common\streams\chunkstream.js:90:8)
    at Request.onend (stream.js:66:10)

的返回数据类型 fileService.createReadStream(shareName, dirPath, fileName);ChunkStream

更新:

这是我更新的有效代码。

    var option = new Object();
    option.disableContentMD5Validation = true;
    option.maximumExecutionTimeInMs = 20 * 60000;
    option.timeoutIntervalInMs = 20 * 6000;

    fileService.getFileToStream(shareName, dirPath, fileName, response, option, function(error, result, res) {
        if(!error) {
            if(res.isSuccessful) {
                console.log(result);
        console.log(res);
        console.log("Success!");
      }
        }
    });

但更频繁地,我遇到了错误。

ERROR
An unhandled exception occurred. Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (http.js:679:11)
at ServerResponse.res.setHeader (D:\home\site\wwwroot\node_modules\express\node_modules\connect\lib\patch.js:59:22)
at ServerResponse.res.set.res.header (D:\home\site\wwwroot\node_modules\express\lib\response.js:518:10)
at addDefaultHeaders (D:\home\site\wwwroot\node_modules\azure-mobile-services\runtime\request\requesthandler.js:590:9)
at ServerResponse. (D:\home\site\wwwroot\node_modules\azure-mobile-services\runtime\request\requesthandler.js:299:13)
at ServerResponse._.wrap as end
at Request.onend (stream.js:66:10)
at Request.EventEmitter.emit (events.js:126:20)
at IncomingMessage.Request.onRequestResponse.strings (D:\home\site\wwwroot\App_Data\config\scripts\node_modules\azure-storage\node_modules\request\request.js:1153:12)
at IncomingMessage.EventEmitter.emit (events.js:126:20)
4

2 回答 2

4

NodeJS 类http.ServerResponse实现Writable Stream接口,请参考 NodeJS API https://nodejs.org/api/http.html#http_class_http_serverresponsehttps://nodejs.org/api/stream.html#stream_class_stream_writable_1

因此,当您将数据流发送到 NodeJS 的响应中时,您只需在示例代码“getFileToStream” http://azure.github.io/azure-storage-node/#toc8中使用对象response而不是流编写器。fs.createStreamWriter(...)

这是我的示例代码如下:

var http = require('http');
var azure = require('azure-storage');
var fileService = azure.createFileService('<storage_key_name>','<storage_access_key>');

http.createServer(function (request, response) {
    var shareName = request.headers.sharename;
    var dirPath = request.headers.directorypath;
    var fileName = request.headers.filename;
    response.setHeader('Content-Type', 'application/json');
    fileService.getFileToStream(shareName, dirPath, fileName, response, {disableContentMD5Validation: true}, function(error, result, response) {
            if(!error) {
                    //console.log(result);
                    //console.log(response);
                   if(response.isSuccessful) {
                                console.log("Success!");
                   }
            }
    });
}).listen(1337, "127.0.0.1");

console.log('Server running at http://127.0.0.1:1337/');

最好的祝福。


从 Azure 文件存储中获取大于 4MB 的文件,有一个请求头x-ms-range-get-content-md5会导致状态码400(Bad Request) 错误,请参考Get FileAzure 文件存储的 REST API 文档https://msdn.microsoft.com/ en-us/library/azure/dn194274.aspx,见下文:

在此处输入图像描述

因此,我查看了适用于 Node 的 Azure 文件存储 SDK 的来源(https://github.com/Azure/azure-storage-node/blob/master/lib/services/file/fileservice.js)。对于函数getFileToText、和getFileToLocalFile,您需要设置属性以避免错误,见下文。createReadStreamgetFileToStreamoptions.disableContentMD5Validation

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

并参考getFileToStream例如的来源:

在此处输入图像描述

在我的示例代码中,需要{disableContentMD5Validation: true}在调用函数的前面添加代码作为选项getFileToStream

于 2015-10-20T07:12:34.447 回答
1

您可能想以这种方式尝试:

exports.get = function(request, response) {
  var shareName = request.headers.sharename;
  var dirPath = request.headers.directorypath;
  var fileName = request.headers.filename;

  var fileService = azure.createFileService();
  var readStream = fileService.createReadStream(shareName, dirPath, fileName);    

  var dataLength = 0;
  readStream.on('data', function (chunk) {
    dataLength += chunk.length;
  })

  readStream.on('end', function(){
    response.setHeader('Content-Type', 'application/json');
    response.setHeader('Content-Length', dataLength);
  });

  readStream.pipe(response);

  response.on('finish', function (chunk) {
    response.send(statusCodes.OK, JSON.stringify("Success!"));
  })
}
于 2015-10-20T05:36:36.543 回答