Node.js 从GCP bucket下载文件会返回一个带有nodeJS的空文件
当我运行Node.js 从GCP bucket下载文件会返回一个带有nodeJS的空文件,node.js,json,express,google-cloud-platform,bucket,Node.js,Json,Express,Google Cloud Platform,Bucket,当我运行node downloadFile.js时,我使用NodeJs从我的gcp bucket下载一个Json文件,但是当我使用express时,使用postman向路由发出请求,然后执行该文件,它会创建一个文件,但它是空的 这很奇怪,express和它有什么关系吗?它需要权限还是什么 这是我的密码 const gc = require('./config') const bucket = gc.bucket('my_bucket') // nombre del bucket en GCS /
node downloadFile.js
时,我使用NodeJs从我的gcp bucket下载一个Json文件,但是当我使用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
}
解决方案很简单,但很棘手。我使用nodemon启动服务器,因此当下载并创建文件时,nodemon将其视为文件中的更改,因此它重新启动了服务器,因此没有时间写入文件
简而言之,只需使用
节点索引.js
或节点服务器.js
如果这对您没有帮助,请检查您的IAM权限,在bucket或项目中查找,并确保您可以编辑、删除、创建等。