Firebase 功能 - 在上传到存储时调整大小并覆盖现有图像 [英] Firebase Function - Resize and overwrite existing image on upload to Storage

查看:15
本文介绍了Firebase 功能 - 在上传到存储时调整大小并覆盖现有图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我遵循了谷歌官方示例 用于创建 Cloud Storage 触发的 Firebase 函数,该函数将从上传的图像创建调整大小的缩略图并将它们也上传到 Storage.这里是简化的:

So I have followed the Google's official sample for creating a Cloud Storage triggered Firebase Function that will create resized thumbnails from uploaded images and upload them to the Storage as well. Here it is simplified:

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_"前缀来避免这种情况.

However, when the new thumbnail uploads, the function gets triggered again and so forth in a loop. They have avoided that by returning if the uploaded file has a "thumb_" prefix.

然后你会得到两张图像(原始图像和缩略图),我想用缩略图重写现有图像,所以我只有一张带有原始路径的图像.

You then end up with two images (the original and the thumbnail) and I want to rewrite the existing image with the thumbnail so I only have one image with the original path.

我不知道该怎么做,因为我不知道如何在不更改名称的情况下避开重新上传循环.我可以在上传缩略图后删除原始图像,但指向原始图像的链接已经返回并保存在实时数据库中(这些图像是用户的个人资料图片).

I don't know how to go about this because I don't know how to evade the reupload loop without a name change. I can delete the original image after uploading the thumbnail but the link pointing to the original image is already returned and saved in the Realtime Database (these images are profile pictures for users).

推荐答案

查看 @google-cloud/storage npm 模块中的 bucket.js 文档后,我终于设法用缩略图覆盖了原始文件/路径,并避免了循环,

After looking at the bucket.js documentation in the @google-cloud/storage npm module, I have finally managed to overwrite the original file/path with the thumbnail image and also avoid the loop,

可以通过在上传缩略图时附加自定义元数据,并在下次触发函数时测试该元数据来完成.

It can be done by attaching custom metadata when uploading the thumbnail, and testing for that metadata when the function triggers the next time.

我只会发布我所做的更改,其余与链接示例中的相同.

I will just post the changes I made, the rest is the same as in the linked sample.

这是测试:

const filePath = event.data.name
const metadata = event.data.metadata

if (metadata.isThumb) {
    console.log('Exiting: Already a thumbnail')
    return
}

这是 spawn 承诺返回的部分:

And here's the part when the spawn promise returns:

    return spawn(/* ... */)
}).then(_ => {
    console.log('Thumbnail created locally.')
    metadata.isThumb = true  // We add custom metadata
    const options = {
        destination: filePath,  // Destination is the same as original
        metadata: { metadata: metadata }
    }
    // We overwrite the (bigger) original image but keep the path
    return bucket.upload(/* localThumb */, options)
})

这篇关于Firebase 功能 - 在上传到存储时调整大小并覆盖现有图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆