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

查看:17
本文介绍了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.

它适用于小文件.

但是如果我上传一个大文件,那么函数会因为显示这个错误而终止

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