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

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

问题描述

所以我遵循了 Google的官方示例,用于创建云存储触发的Firebase功能,该功能将根据上传的图像创建调整大小的缩略图,并将其上传到存储。这里简化了:

  exports.generateThumbnail = functions.storage.object()。onChange(event => {

//获取上传的文件数据(存储桶,名称,类型......)

//如果文件不是图像或名称以thumb_开头,则返回

//将上传的图像下载到临时本地文件中,
//使用ImageMagick调整大小
//将其上传到存储,名称为thumb_< filename>

}

然而,当新的缩略图上传时,该功能会再次被触发,因此如果上传的文件有一个thumb_前缀,他们可以通过返回来避免这种情况。



然后你得到两张图片(原件和缩略图) )我希望用缩略图重写现有图像,所以我只有一张原始路径的图像。



我不知道如何去做,因为我不知道如何逃避reuploa d循环没有名称更改。我可以在上传缩略图后删除原始图像,但指向原始图像的链接已经返回并保存在实时数据库中(这些图像是用户的个人资料图片)。

@ google-cloud / storage npm模块,我终于设法用缩略图覆盖了原始文件/路径,也避免了循环,



可以通过附加自定义来完成上传缩略图时的元数据,以及下次函数触发时对元数据的测试。



我将发布我所做的更改,其余部分与中的相同链接样本。



以下是测试:

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

if(metadata.isThumb){
console.log('退出:已经是缩略图')
返回
}

这是 spawn 承诺退货:

  return spawn(/ * ... * /)
})。then(_ = > {
console.log('本地创建缩略图'。)
metadata.isThumb = true //我们添加自定义元数据
const options = {
destination:filePath,// Destination与原始
元数据相同:{metadata:metadata}
}
//我们覆盖(更大)原始图像但保留路径
返回bucket.upload(/ * localThumb * /,options)
})


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>"

}

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).

解决方案

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.

Here's the testing:

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

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

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天全站免登陆