Home > database >  Unable to write file in proper format after reading from the S3 url using nodejs
Unable to write file in proper format after reading from the S3 url using nodejs

Time:12-08

I am trying to write excel file after reading from the S3 bucket url with the help of axios data is written into the file but its not in readable format. I want to write data in proper excel file format format in which it was being uploaded.

Below is my code:

 axios({
            url: 'https://example.com/1666960010753_Me Data.xlsx',
            method: 'GET',
            responseType: 'blob',
        }).then((response) => {

            const ostream = fs.createWriteStream(`./${filename}`);
            ostream.write(response.data);
           
        });

Someone let me know what I am doing wrong.

CodePudding user response:

First off, your Excel data sheet is now publicly available. Don't know if this is on purpose, but if not, please remove it from your code example.


Now to the question.

You should use an 'arraybuffer' as response type instead of 'blob'. This answer explains it really well why that is.

Also, you should end your stream. This indicates that the stream is finished an no data should and can be written to the stream again.

And to finish it off, you should set the encoding of the write stream to 'binary'. The array buffer is a binary buffer, and it's always best practice when you know what data you're going to get, to set the encoding for a file.


For your code, it would look like this:

axios({
  url: 'https://<s3-domain>/<path-to-excel-file>.xlsx',
  method: 'GET',
  responseType: 'arraybuffer',
}).then((response) => {
  const ostream = fs.createWriteStream(`./${filename}`, 'binary');
  ostream.pipe(response.data);
  ostream.end();
});
  • Related