从我们上面的评论来看,您的问题似乎是因为您“没有检查正确的文件名”
fileName.startsWith(THUMB_PREFIX)
userkey + -size- + timestamp
模式。
但是,在代码中还有一个问题:您正在使用
sizes.forEach((size, index) => {})
原始的云函数代码(来自Firebase官方示例)链接了几个异步操作,以便生成缩略图。原始的云函数代码只返回一个承诺,即在链的末尾解析的承诺。
你可以沿着这条路线做点什么。这只是一个
提案初稿
关于如何复制异步操作以并行处理这两个文件。可能有很大的空间来简化代码—避免重复—请注意,它根本没有经过测试!)
return mkdirp(tempLocalDir)
.then(() => {
// Download files from bucket.
const promises = [];
promises.push(file_1.download({ destination: tempLocalFile_1 }));
promises.push(file_2.download({ destination: tempLocalFile_2 }));
return Promise.all(promises);
})
.then(() => {
const promises = [];
promises.push(
spawn(
'convert',
[
tempLocalFile,
'-thumbnail',
`${THUMB_MAX_WIDTH_1}x${THUMB_MAX_HEIGHT_1}>`,
tempLocalThumbFile_1
],
{ capture: ['stdout', 'stderr'] }
)
);
promises.push(
spawn(
'convert',
[
tempLocalFile,
'-thumbnail',
`${THUMB_MAX_WIDTH_2}x${THUMB_MAX_HEIGHT_2}>`,
tempLocalThumbFile_2
],
{ capture: ['stdout', 'stderr'] }
)
);
return Promise.all(promises);
})
.then(() => {
//console.log('Thumbnail created at', tempLocalThumbFile);
// Uploading the Thumbnail.
const promises = [];
promises.push(
bucket.upload(tempLocalThumbFile_1, {
destination: thumbFilePath_1,
metadata: metadata
})
);
promises.push(
bucket.upload(tempLocalThumbFile_1, {
destination: thumbFilePath_1,
metadata: metadata
})
);
return Promise.all(promises);
})
.then(() => {
console.log('Thumbnail uploaded to Storage at', thumbFilePath);
// Once the image has been uploaded delete the local files to free up disk space.
fs.unlinkSync(tempLocalFile_1);
fs.unlinkSync(tempLocalThumbFile_1);
fs.unlinkSync(tempLocalFile_2);
fs.unlinkSync(tempLocalThumbFile_2);
// Get the Signed URLs for the thumbnail and original image.
const config = {
action: 'read',
expires: '03-01-2500'
};
return Promise.all([
thumbFile_1.getSignedUrl(config),
file_1.getSignedUrl(config),
thumbFile_2.getSignedUrl(config),
file_2.getSignedUrl(config)
]);
})
.then(results => {....}}
https://firebase.google.com/docs/functions/video-series/