我希望能够将从谷歌云下载的文件直接发送到客户端,而不必先保存在我的服务器上,然后从我服务器上保存的版本创建一个下载到客户端,因为这会使过程变慢,因为文件下载了两次,首先从谷歌云下载到我自己的服务器,然后从我自己的服务器下载到客户端。
router.get("/:filename", async(req, res) => {
try {
// Grab filename from request parameter
const fetchURL =req.params.filename;
const file = await File.findOne({fetchURL});
const srcFileName = file.originalname;
// Call GCS with bucketName and check the file method with srcFileName and check again with download method which takes download path as argument
storage
.bucket(bucketName)
.file(srcFileName)
.download({
destination: path.join(process.cwd(), "downloads", srcFileName)
})
.then(() =>
res.download(path.join(process.cwd(), "downloads", srcFileName), err =>
err ? console.log(err) : null
)
)
.catch(err =>res.status(400).json({
message: err.message
}));
} catch …Run Code Online (Sandbox Code Playgroud)