发送数据流作为响应 - node.js - 使用 node.js 的 Azure 文件服务
Sending stream of data in response - node.js - Azure File service using node.js
我正在使用 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);
的 return 数据类型是 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)
您可能想这样尝试:
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!"));
})
}
NodeJSClasshttp.ServerResponse
实现了Writable Stream
接口,请参考NodeJSAPIhttps://nodejs.org/api/http.html#http_class_http_serverresponse and https://nodejs.org/api/stream.html#stream_class_stream_writable_1.
所以你只需要在示例代码"getFileToStream" http://azure.github.io/azure-storage-node/#toc8中使用对象response
而不是流编写器fs.createStreamWriter(...)
,当你将数据流发送到响应中时NodeJS.
这是我的示例代码如下:
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 File Storage获取大于4MB的文件,有一个请求头x-ms-range-get-content-md5
会导致状态码400
(Bad Request)错误,请参考Get File
REST API Azure 文件存储文档 https://msdn.microsoft.com/en-us/library/azure/dn194274.aspx,见下文:
所以我查看了 Azure File Storage SDK for Node (https://github.com/Azure/azure-storage-node/blob/master/lib/services/file/fileservice.js) 的源代码。对于函数 getFileToText
、getFileToLocalFile
、createReadStream
和 getFileToStream
,您需要设置 options.disableContentMD5Validation
属性以避免错误,见下文。
- @param {boolean} [options.disableContentMD5Validation] When set to true, MD5 validation will be disabled when downloading files.
并以getFileToStream
的来源为例:
在我的示例代码中,需要在调用函数getFileToStream
前添加代码{disableContentMD5Validation: true}
作为选项。
我正在使用 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);
的 return 数据类型是 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)
您可能想这样尝试:
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!"));
})
}
NodeJSClasshttp.ServerResponse
实现了Writable Stream
接口,请参考NodeJSAPIhttps://nodejs.org/api/http.html#http_class_http_serverresponse and https://nodejs.org/api/stream.html#stream_class_stream_writable_1.
所以你只需要在示例代码"getFileToStream" http://azure.github.io/azure-storage-node/#toc8中使用对象response
而不是流编写器fs.createStreamWriter(...)
,当你将数据流发送到响应中时NodeJS.
这是我的示例代码如下:
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 File Storage获取大于4MB的文件,有一个请求头x-ms-range-get-content-md5
会导致状态码400
(Bad Request)错误,请参考Get File
REST API Azure 文件存储文档 https://msdn.microsoft.com/en-us/library/azure/dn194274.aspx,见下文:
所以我查看了 Azure File Storage SDK for Node (https://github.com/Azure/azure-storage-node/blob/master/lib/services/file/fileservice.js) 的源代码。对于函数 getFileToText
、getFileToLocalFile
、createReadStream
和 getFileToStream
,您需要设置 options.disableContentMD5Validation
属性以避免错误,见下文。
- @param {boolean} [options.disableContentMD5Validation] When set to true, MD5 validation will be disabled when downloading files.
并以getFileToStream
的来源为例:
在我的示例代码中,需要在调用函数getFileToStream
前添加代码{disableContentMD5Validation: true}
作为选项。