【问题标题】:Firebase Functions extremely slow resizing an imageFirebase 功能非常缓慢地调整图像大小
【发布时间】:2017-10-23 10:59:26
【问题描述】:

按照here 提到的示例,我使用了一个 firebase 函数:

1) 将上传的照片调整为两个不同的尺寸

2) 然后该函数将检索 url 并将其写入数据库,以便客户端应用程序可以在需要时检索正确的缩略图。

不幸的是,我的计划从未实现,因为 firebase 功能非常缓慢地完成其操作,而且客户端应用程序等待那么长时间是令人望而却步的。 请记住,我在这里不是在谈论函数冷启动...无论图像大小或上传的图像数量如何,该函数在每次执行时都会变慢。

这有什么特别的原因吗?我是不是做错了什么??

这是我的代码:

exports.generateThumbnailImages = storageRef.onChange(event => {

const object = event.data; 
const fileBucket = object.bucket; 
const filePath = object.name; 
const contentType = object.contentType; 
const resourceState = object.resourceState; 
const metageneration = object.metageneration; 

var folder=filePath.split('/')[0];
var fileName=filePath.split('/')[1];
// console.log(folder);
if (folder){
    if (folder.startsWith('images') && !fileName.includes('thumb')) {
        if(resourceState=='not_exists'){
            console.log('THIS IS A DELETION EVENT '+fileName);
        }
        else if(resourceState==='exists' && metageneration>1){
            console.log('THIS IS A METAGENERATION EVENT '+fileName);
        }   
        else if(resourceState==='exists'){
            console.log('THIS IS A CREATION EVENT '+fileName);
            const bucket = gcs.bucket(fileBucket);
            const tempFilePath = '/tmp/${fileName}';
            const tempFilePathBig = '/tmp/${fileName}'+'Big';   
            bucket.file(filePath).download({destination: tempFilePath}).then(function() {
                bucket.file(filePath).download({destination: tempFilePathBig}).then(function() {
                    console.log('IMAGE DOWNLOADED LOCALLY TO', tempFilePath); 
                spawn('convert', [tempFilePath, '-thumbnail', '200x200', tempFilePath]).then(function() {
                    spawn('convert', [tempFilePathBig, '-thumbnail', '400x400', tempFilePathBig]).then(function() {
                        console.log('THUMBNAIL CREATED AT', tempFilePath);  
                    const thumbFilePath = filePath.replace(/(\/)?([^\/]*)$/, '$1thumb_$2');     
                    const thumbFilePathBig = filePath.replace(/(\/)?([^\/]*)$/, '$1thumb_Big$2');       
                    bucket.upload(tempFilePath, {destination: thumbFilePath}).then(function(){
                        bucket.upload(tempFilePathBig, {destination: thumbFilePathBig}).then(function(){
                            return;
                        });
                    });
                });
                });
            });
        });  
        }`

【问题讨论】:

  • 你看到了什么表现?你要调整什么类型的图像?输入输出大小是多少?
  • @Frank 根据函数日志,我测得的平均输出(调整大小完成)约为 2-3 分钟(!)。我尝试了不同的图像格式,但主要是 jpg 和 png。输入大小也会因图像而异,但为了测试起见,假设一个 jpg,1920x1080 大小为 240kb,我们需要两个 200x200 和 400x400 的缩略图(尽管如果图像不是矩形,则会发生缩放,因此调整了这些尺寸)跨度>
  • 您找到解决方案了吗?我也有同样的问题。据我所知,spawn 返回的承诺不尊重解析并等待超时?
  • @CodeKiwi 帮助您完成此任务的最简单方法是观看这两个火报:1:youtube.com/watch?v=3lzEgwiSs-M 2:youtube.com/watch?v=pDLpEn3PbmE 使用提供的代码作为基础,然后对其进行详细说明。它按预期工作!
  • @GiorgosS。感谢您的提醒,但我没有找到任何有帮助的东西。我已经在做所有应该做的事情了。任何其他建议都将不胜感激。

标签: javascript firebase imagemagick firebase-storage google-cloud-functions


【解决方案1】:

您可以尝试将两个spawn 命令合并为一个,而不是两次下载/处理同一个图像。

bucket.file(filePath).download({destination: tempFilePath}).then(function() {
    spawn('convert', [tempFilePath,
                      '-thumbnail', '400x400',
                      '-write', $tempFilePath400,
                      '-thumbnail', '200x200',
                      '-write', $tempFilePath200,
                      'null:']).then(function() {
        // ...

我还建议探索基于队列的后台工作程序;这样,客户端应用程序就会知道工作已被推迟。

【讨论】:

  • 感谢您的建议,但问题不在于客户是否会被告知潜在的延迟(或者是否有东西在队列中),而是关于使用 firebase 功能的实际速度。我也尝试使用一个 spawn 命令调整大小,不幸的是结果是一样的……大约 2-3 分钟。欢迎任何其他建议!
猜你喜欢
  • 2018-01-18
  • 2019-01-03
  • 2012-10-03
  • 2020-07-30
  • 1970-01-01
  • 1970-01-01
  • 2018-09-09
  • 2011-02-21
  • 1970-01-01
相关资源
最近更新 更多