【发布时间】:2022-01-31 03:44:45
【问题描述】:
我有这段代码可以创建一个签名 URL 来下载我的存储桶中的特定文件:
exports.download = functions.https.onRequest((req, res) => {
const userID = req.query.userID;
const downloadID = req.query.downloadID;
const fileName = req.query.fileName;
admin
.storage()
.bucket()
.file(`userUploads/${userID}/${downloadID}/${fileName}`)
.getSignedUrl({
version: "v4",
action: "read",
expires: Date.now() + 15 * 60 * 1000, // 15 minutes
})
.then((signedURLArray) => {
const url = signedURLArray[0];
// return res.download(url);
return res.redirect(url);
})
.catch((err) => {
return res.send(err);
});
});
我试过了:
res.setHeader(
`"content-disposition", "attachment; filename=${fileName}"`
);
与 res.redirect 但它只是给了我一个 HTTP_TOKEN_ERROR 。如果我使用res.redirect() 而不设置任何额外的标题,它只会在浏览器上查看文件,而不是下载它 - 我只希望用户能够下载文件。
显然,res.download 不起作用,因为相关文件不在服务器上,它在我的存储桶文件所在的其他服务器上提供(很可能是一些 storage.googleapis.com 域)
更新:
从响应请求中可以看出:
它将正确的数据发送回浏览器
【问题讨论】:
-
我们试试
res.header('Content-disposition', "attachment; filename=" + fileName); -
@hoangdv 它只是再次查看文件它没有下载它
标签: node.js firebase express google-cloud-functions google-cloud-storage