Firebase云功能[错误:超出内存限制.函数调用被中断.]在youtube视频上载 [英] Firebase cloud function [ Error: memory limit exceeded. Function invocation was interrupted.] on youtube video upload

查看:115
本文介绍了Firebase云功能[错误:超出内存限制.函数调用被中断.]在youtube视频上载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试使用Firebase云功能将视频上传到youtube.

I was trying to upload videos to youtube using the firebase cloud function.

我需要的是,当用户将视频上传到Firebase云存储时,functions.storage.object().onFinalize事件将被触发,并且在该事件中,我将文件存储到一个临时位置并将文件上传到youtube从临时位置到youtube,在上传后我删除了这两个文件.

What I need is when a user uploads a video to firebase cloud storage, functions.storage.object().onFinalize event will get triggered and in that event, I store the file to a temporary location and upload the file to youtube from the temp location to youtube, after uploading I delete both files.

对于小文件,它会很好地工作.

It will work fine for small files.

但是,如果我上传了一个大文件,则该功能会由于显示此错误而终止

But if I upload a large file then the function is getting terminated by showing this error

错误:超出内存限制.函数调用被中断.

上传视频的代码

   var requestData = {
        'params': {
        'part': 'snippet,status'
        },
        'properties': {
        'snippet.categoryId': '22',
        'snippet.defaultLanguage': '',
        'snippet.description': "docdata.shortDesc",
        'snippet.tags[]': '',
        'snippet.title': "docdata.title",
        'status.embeddable': '',
        'status.license': '',
        'status.privacyStatus': 'public',
        'status.publicStatsViewable': ''
        }, 'mediaFilename': tempLocalFile
    };

    insertVideo(tempLocalFile, oauth2Client, requestData);

插入视频功能

function insertVideo( file, oauth2Client, requestData) {
    return new Promise((resolve,reject)=>{
        google.options({ auth: oauth2Client });
        var parameters = removeEmptyParameters(requestData['params']);
        parameters['auth'] = oauth2Client;
        parameters['media'] = { body:  fs.createReadStream(requestData['mediaFilename'])};
        parameters['notifySubscribers'] = false;
        parameters['resource'] = createResource(requestData['properties']);

        console.log("INSERT >>> ");
        let req = google.youtube('v3').videos.insert(parameters,  (error, received)=> {
            if (error) {
                console.log("in error")
                console.log(error);
                try {
                    fs.unlinkSync(file);
                } catch (err) {
                    console.log(err);
                } finally{
                    // response.status(200).send({ error: error })
                }
                reject(error)
            } else {
                console.log("in else")
                console.log(received.data)
                fs.unlinkSync(file);
                resolve();
            }
        }); 
    })

}

用于创建临时本地文件的代码

code for creating temp local file

           bucket.file(filePath).createReadStream()
            .on('error', (err)=> {
                reject(err)
            })
            .on('response', (response)=> {
                console.log(response)
            })
            .on('end', ()=> {
                console.log("The file is fully downloaded");
                resolve();
            })
            .pipe(fs.createWriteStream(tempLocalFile));

每个文件的读写都由流处理,任何关于为什么发生内存问题的想法

Every file read and write is handled by streams, any idea on why the memory issue is happening

推荐答案

Cloud Functions中文件系统的唯一可写部分是/tmp目录.根据文档此处:

The only writeable part of the filesystem in Cloud Functions is the /tmp directory. As per the documentation here:

这是一个本地磁盘安装点,称为"tmpfs"卷,其中 写入卷的数据存储在内存中.请注意,它将 消耗为该功能配置的内存资源.

This is a local disk mount point known as a "tmpfs" volume in which data written to the volume is stored in memory. Note that it will consume memory resources provisioned for the function.

这就是为什么您使用更大的文件来达到内存限制的原因.

This is why you hit the memory limit with bigger files.

您的选择是:

  • 为您的功能分配更多内存(当前最多2 GB)
  • 从可以写入文件系统的环境中执行上载.例如,您的Cloud Function可以调用App Engine Flexible服务来执行上传.

这篇关于Firebase云功能[错误:超出内存限制.函数调用被中断.]在youtube视频上载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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