Reputation: 448
I am trying to download the files stored in azure blob storage using the url to the individual blobs.
I am downloading five blob files and i do have the separate urls to all those blob files.
Below is my code:
function Download()
{
const fs = require('fs');
const path = require('path');
const https = require('https');
const url = require('url');
var sasToken = '?sv=2019-02-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx2019-10-3xxxxxxxZ&sp=rl'
var blobUris = ['https:xxx', 'xxx', 'xxx', 'xxx', 'xxx'];
var i;
for(i=0; i<blobUris.length; i++)
{
var sasUrl = blobUris[i] + sasToken;
var filename=url.parse(blobUris[i]).pathname.split('/').pop();
var filePath = "D:/BLESensor"
var file = fs.createWriteStream(filePath+"/"+filename)
var request = https.get(sasUrl, function(response) {
response.pipe(file);
// Promise.resolve()
// .then(() => file.destroy())
// .catch(console.log)
});
}
}
return Download();
I am getting the below error when i run the function:
Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at writeAfterEnd (_stream_writable.js:248:12)
at WriteStream.Writable.write (_stream_writable.js:296:5)
at IncomingMessage.ondata (_stream_readable.js:709:20)
at IncomingMessage.emit (events.js:198:13)
at IncomingMessage.Readable.read (_stream_readable.js:504:10)
at flow (_stream_readable.js:973:34)
at resume_ (_stream_readable.js:954:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
Emitted 'error' event at:
at errorOrDestroy (internal/streams/destroy.js:107:12)
at WriteStream.onerror (_stream_readable.js:733:7)
at WriteStream.emit (events.js:203:15)
at errorOrDestroy (internal/streams/destroy.js:107:12)
at writeAfterEnd (_stream_writable.js:250:3)
at WriteStream.Writable.write (_stream_writable.js:296:5)
[... lines matching original stack trace ...]
at flow (_stream_readable.js:973:34)
Any suggestions would be much appreciated. Thanks!
Upvotes: 2
Views: 6621
Reputation: 30715
This is due to the fact that we're downloading in a for loop while the https.get call (like all Node.js I/O calls) is asynchronous. So response.pipe is not going to work correctly in this case (since the file stream will be closed by the time response.pipe is called) with a function scoped file variable (e.g. var file..)
There is simple fix for this issue, we just need to ensure that the file variable is block scoped, e.g. use either let or const for this.
function Download()
{
const fs = require('fs');
const path = require('path');
const https = require('https');
const url = require('url');
var sasToken = '?sv=2019-02-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx2019-10-3xxxxxxxZ&sp=rl'
var blobUris = ['https:xxx', 'xxx', 'xxx', 'xxx', 'xxx'];
var i;
for(i=0; i<blobUris.length; i++)
{
var sasUrl = blobUris[i] + sasToken;
var filename=url.parse(blobUris[i]).pathname.split('/').pop();
var filePath = "D:/BLESensor"
// Make sure file variable is block scoped. We could use const too.. but not var!
let file = fs.createWriteStream(filePath+"/"+filename)
console.log("Downloading file: " + (i+1));
var request = https.get(sasUrl, function(response) {
response.pipe(file);
});
}
}
return Download();
Upvotes: 5