How to pass Duplex Stream in formdata?

1k Views Asked by At

Image saved in s3 is downloaded as a stream and it is piped to duplex stream. The stream which is coming from s3 should be sent in formdata to another remote URL which when done with fs.createReadStream works but not with the duplex stream.

code

const download = (url, duplex) => {
  axios
    .request({
      url,
      method: "GET",
      responseType: "stream",
    })
    .then((response) => {
      response.data.pipe(duplex);
    });
};

const mainFunction = () => {
  const duplex = new PassThrough();
  download("some_url", duplex);

  // duplex.pipe(writeStream) works and writes to file
  // send formdata
  const form = new FormData();
  form.append("file", duplex); // should work? as it is duplex

  // this does not work
  await axios.post(url, form, {
    "Content-Type": "multipart/form-data",
    ...form.getHeaders(),
  });
};
1

There are 1 best solutions below

0
kailash yogeshwar On

So here is what was missing and finally found it. Some destination expects file mimeType when you're sending stream in formdata.

Solution

const download = (url, duplex) => {
  axios
    .request({
      url,
      method: "GET",
      responseType: "stream",
    })
    .then((response) => {
      response.data.pipe(duplex);
    });
};

const mainFunction = () => {
  const duplex = new PassThrough();
  download("some_url", duplex);

  const form = new FormData();
  // heres what was missing 
  form.append("file", duplex, {
    filename: 'hola.jpg',
    contentType: 'image/jpeg'
  }); // Voila! It works.

  await axios.post(url, form, {
    'Content-Type': "multipart/form-data",
    ...form.getHeaders(),
  });
};