Firebase功能极其缓慢调整图像大小

时间:2017-05-23 13:03:36

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

按照提到的示例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;
                        });
                    });
                });
                });
            });
        });  
        }`

1 个答案:

答案 0 :(得分:0)

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

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

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

相关问题