【发布时间】:2020-11-03 10:40:07
【问题描述】:
从 firebase 函数上传文件后,我正在生成一个签名的 url,但这需要 5 秒的时间。我的主要工作在 5 秒内完成,但链接生成增加了更多等待用户的时间。
这是我使用的功能:
function getSignedUrl(file) {
console.time('URLGenerated ');
/**
* Handle url generation url to properly handle user veiws
* Crete catch error to see if it is logged properly
*/
let options = { action: 'read', expires: Date.now() + 5 * 60 * 1000 }; // 5min Expiration Time
let bucketFileName = path.join(crypto.randomBytes(12).toString('hex'), path.basename(file));
return bucket.upload(file, { destination: `public/${bucketFileName}` })
.then(() => {
return bucket.file(`public/${bucketFileName}`).getSignedUrl(options)
.then((urls) => {
fs.unlinkSync(file);
console.timeEnd('URLGenerated ');
return urls[0];
})
.catch((e) => {
console.log('Link Generation Error' + e);
});
}).catch((e) => console.log(e));
}
文件大小约为 200 - 350 KB。
上一代用了 3.5 秒,这很关键。
发生了什么事?如何减少时间?
【问题讨论】:
标签: node.js firebase google-cloud-functions google-cloud-storage