0

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%20Data.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.

Digvijay
  • 2,887
  • 3
  • 36
  • 86

1 Answers1

1

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();
});
Bart Versluijs
  • 186
  • 1
  • 8
  • HI, is there any way so that without writing stream in some file I can directly send it as a JSON response. – Digvijay Dec 08 '22 at 05:45
  • What do you mean with "send it as JSON response"? The file isn't JSON. If you meant how you can pipe it to your response, some extra information is needed (are you using Express for example or some other framework?) – Bart Versluijs Dec 08 '22 at 08:26