How to download a file from a server, and and post it again in another one (Node.js)

789 Views Asked by At

In my node application, I want to get a file from one server, and then upload it into another server. I have the following code:

const axios = require("axios");
const FormData = require("form-data");
const { createWriteStream, createReadStream } = require("fs");

const response = await axios({
  url: "https://first-server/image.png",
  method: "GET",
  responseType: "stream",
});

await new Promise((res) => {
  response.data.pipe(
    createWriteStream("someFile.png").on("finish", () => {
      res();
    })
  );
});

const form = new FormData();
form.append("file", createReadStream("./someFile.png"));

const postHeaders = {
  headers: {
    Authorization: "Bearer " + env("APY_KEY"),
    ...form.getHeaders(),
  },
  data: form,
};

axios.post("https://second-server.com/api", form, postHeaders)
  .then((response) => {
    console.log(JSON.stringify(response.data));
  })

This code works, but I think it's not the right way to do this, since it writes the retrieved file into the local disc before posting it again into the second server. I need to be able to upload the file without writing it into the local disc. Is there any way?

2

There are 2 best solutions below

1
On BEST ANSWER

Just replace form.append("file", createReadStream("./someFile.png")); with form.append("file", response.data);

Both response.data and createReadStream("./someFile.png") are readable stream.

1
On

Note: You can directly transfer returned stream data without any need to create temporary file.

const axios = require("axios");
const FormData = require("form-data");

axios({
  url: "http://localhost:3000/temp.png",
  method: "GET",
  responseType: "stream",
}).then(response => {

    response.data.on("data", function(data) {
        const form = new FormData();
        form.append("file", data);

        const postHeaders = {
          headers: {
            // Authorization: "Bearer " + env("APY_KEY"),
            ...form.getHeaders(),
          },
          data: form,
        };

        axios.post("http://localhost:8000/api", form, postHeaders)
        .then((response) => {
            // console.log(JSON.stringify(response.data));
        })
        .catch(function(error){
            console.log(error)
        });
    });

})
.catch(function(error){
    console.log(error)
});