Cesar Tocon
Cesar Tocon

Reputation: 11

Downloading a file from GCP bucket returns an empty file with nodeJS

I'm using NodeJs to download a Json file from my gcp bucket, when I run node downloadFile.js works perfectly fine, but when I do it with express, using postman to make the request to the route and then executing the file it creates a file but its empty.

Its weird, does express has to do something with it?, does it need permissions or something?

this is my code

const gc = require('./config')
const bucket = gc.bucket('my_bucket') // nombre del bucket en GCS
// The ID of your GCS bucket
const downloadJsonFile = async (req, res) => {
  fileName=req.body.fileName
  const bucketName = "my_bucket";

  // The path to which the file should be downloaded  
  console.log("dirname de download",__dirname)
  const destFileName = "./public/result.json"
  
  // Creates a client  

  async function downloadFile() {
    const options = {
      destination: destFileName,
    };

    // Downloads the file
    await bucket.file(fileName).download(options);

    console.log(
      `gs://${bucketName}/${fileName} downloaded to ${destFileName}.`
    );
  }

  downloadFile().catch(console.error);  
};

module.exports={
    downloadJsonFile
}

Upvotes: 0

Views: 804

Answers (1)

Cesar Tocon
Cesar Tocon

Reputation: 11

The solution was so simple but tricky. I was using nodemon to start the server, so when the file was downloaded and created nodemon took it as a change in the files, so it restarted the server, therefore was no time to write in to the file.

In a nutshell just use node index.js or node server.js

Upvotes: 1

Related Questions