所以我按照Google官方示例创建了一个Cloud Storage触发的Firebase函数,该函数将从上传的图像创建调整大小的缩略图并将其上传到Storage中。这是简化后的代码:
exports.generateThumbnail = functions.storage.object().onChange(event => {
// get the uploaded file data (bucket, name, type...)
// return if the file is not an image or name begins with "thumb_"
// download the uploaded image in a temporary local file,
// resize it using ImageMagick
// upload it to storage with the name "thumb_<filename>"
}
然而,当新的缩略图上传时,该函数再次被触发,从而形成循环。他们通过检查上传的文件是否具有“thumb_”前缀来避免这种情况。
然后,您将获得两个图像(原始图像和缩略图),我希望用缩略图重写现有图像,以便只有一张带有原始路径的图像。
我不知道如何做到这一点,因为我不知道如何在没有名称更改的情况下避免重新上传循环。我可以在上传缩略图后删除原始图像,但指向原始图像的链接已经返回并保存在实时数据库中(这些图像是用户的个人资料图片)。