我正在使用NodeJs从我的gcp存储桶中下载一个Json文件,当我运行node downloadFile.js
时工作得很好,但是当我使用express做这件事时,使用postman向路由发出请求,然后执行文件,它创建了一个文件,但它是空的。
奇怪的是,express需要对它做些什么吗?它需要权限还是什么?
这是我的代码
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
}
发布于 2021-04-16 05:02:30
解决方案很简单,但却很棘手。我使用nodemon启动服务器,所以当文件下载并创建时,nodemon将其视为文件中的更改,因此它重新启动了服务器,因此没有时间写入文件。
简而言之,只要使用node index.js
或node server.js
即可
https://stackoverflow.com/questions/67117688
复制相似问题