【发布时间】:2016-11-29 14:32:56
【问题描述】:
我在 Azure Functions 中运行了以下代码(代码来自 Stack Overflow here),大部分时间都按预期下载大文件。但是,有时它只是停止将数据添加到文件中并且永远不会重新开始。文件越大,发生的频率越高。我没有收到任何错误,什么都没有。有没有什么方法可以在 10 秒后再次唤醒这个过程,比如 10 秒没有进展,或者有什么其他的方法来关注这个过程?
var azure = require('azure-storage');
var fs = require('fs');
module.exports = function (context, input) {
context.done();
var accessKey = 'myaccesskey';
var storageAccount = 'mystorageaccount';
var containerName = 'mycontainer';
var blobService = azure.createBlobService(storageAccount, accessKey);
var recordName = "a_large_movie.mov";
var blobName = "standard/mov/" + recordName;
var blobSize;
var chunkSize = (1024 * 512) * 8; // I'm experimenting with this variable
var startPos = 0;
var fullPath = "D:/home/site/wwwroot/myAzureFunction/input/";
var blobProperties = blobService.getBlobProperties(containerName, blobName, null, function (error, blob) {
if (error) {
throw error;
}
else {
blobSize = blob.contentLength;
context.log('Registered length: ' + blobSize);
fullPath = fullPath + recordName;
console.log(fullPath);
doDownload();
}
}
);
function doDownload() {
var stream = fs.createWriteStream(fullPath, {flags: 'a'});
var endPos = startPos + chunkSize;
if (endPos > blobSize) {
endPos = blobSize;
context.log('Reached end of file endPos: ' + endPos);
}
context.log("Downloading " + (endPos - startPos) + " bytes starting from " + startPos + " marker.");
blobService.getBlobToStream(
containerName,
blobName,
stream,
{
"rangeStart": startPos,
"rangeEnd": endPos-1
},
function(error) {
if (error) {
throw error;
}
else if (!error) {
startPos = endPos;
if (startPos <= blobSize - 1) {
doDownload();
}
}
}
);
}
};
【问题讨论】:
-
您是否尝试过将 chunkSize 从 4MB 减少到 1MB?
-
不是 1 MB。不过,我从 512 KB 的块大小开始,它给出了与上面提到的相同的偶尔超时
-
嗨@GauravMantri - 我将块更改为1MB,它确实使它变得更好。但是,有时大文件仍然会超时,因此问题并没有消失。但它确实改善了它。块大小和可能的超时之间有什么联系?
-
块大小和超时可能与您的 Internet 连接有关。如果您的连接速度较慢,则上传 4MB 的块会失败,而较小的块会成功。