Reputation: 11
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
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