最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why am I getting the error "Error [ERR_STREAM_WRITE_AFTER_END]: write after end" when downloading

programmeradmin3浏览0评论

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 IningMessage.ondata (_stream_readable.js:709:20)
    at IningMessage.emit (events.js:198:13)
    at IningMessage.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!

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 IningMessage.ondata (_stream_readable.js:709:20)
    at IningMessage.emit (events.js:198:13)
    at IningMessage.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!

Share Improve this question edited Oct 30, 2019 at 8:13 Liam 29.8k28 gold badges139 silver badges203 bronze badges asked Oct 30, 2019 at 6:45 Harshith RHarshith R 4482 gold badges13 silver badges33 bronze badges 1
  • 1 There's not enough information here but I suspect that you will solve your problem as you learn the fundamentals of asynchrounous programming in JavaScript. – Aluan Haddad Commented Oct 30, 2019 at 6:49
Add a ment  | 

1 Answer 1

Reset to default 5

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();

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论