我正在尝试将图像上传到Firebase,然后生成2个缩略图。我能够做到这一点没有问题。我目前的障碍是,当我将URL写入实时数据库时,我总是得到与初始上传相同的URL。例如:第一次上传时,我上传的图像带有该图像的两个适当的缩略图第2次上传我得到的上传的图像带有前两个缩略图(第一个图像)第三次上传时,我得到的上传的图片带有第一个图片的缩略图... ...这将继续复制第一个上传的网址在我的存储中,正在生成正确的缩略图,但是URL始终是第一次上传吗?我不知道这是否是getSignedUrl()的问题,真的不知道这里发生了什么。这是我的云函数: export const generateThumbs = functions.storage .object() .onFinalize(async object => { const bucket = gcs.bucket(object.bucket); // The Storage object. // console.log(object); console.log(object.name); const filePath = object.name; // File path in the bucket. const fileName = filePath.split('/').pop(); const bucketDir = dirname(filePath); const workingDir = join(tmpdir(), 'thumbs'); const tmpFilePath = join(workingDir, 'source.png'); if (fileName.includes('thumb@') || !object.contentType.includes('image')) { console.log('exiting function'); return false; } // 1. ensure thumbnail dir exists await fs.ensureDir(workingDir); // 2. Download Sounrce fileName await bucket.file(filePath).download({ destination: tmpFilePath }); //3. resize the images and define an array of upload promises const sizes = [64, 256]; const uploadPromises = sizes.map(async size => { const thumbName = `thumb@${size}_${fileName}`; const thumbPath = join(workingDir, thumbName); //Resize source image await sharp(tmpFilePath) .resize(size, size) .toFile(thumbPath); //upload to gcs return bucket.upload(thumbPath, { destination: join(bucketDir, thumbName), metadata: { contentType: 'image/jpeg' } })
添加回答
举报
0/150
提交
取消