Search code examples
node.jsfirebasegoogle-cloud-functionsmp4fs

Read .mp4 file from firebase storage using fs to send that video to tenserflow model


my graduation project is to convert video into text. I'm trying to read video uploaded in Firebase storage & sent from android app, to send it to TenserFlow model. but I can't read the video.

here is my function:

exports.readVideo = functions.storage
.object()
.onFinalize(async (object) => {
    const bucket = admin.storage().bucket(object.bucket);
    const tempFilePath = path.join(os.tmpdir(), object.name);
    console.log(tempFilePath);
    console.log('download');
    // note download
    await bucket
        .file(object.name!)
        .download({
            destination: tempFilePath,
        })
        .then()
        .catch((err) => {
            console.log({
                type: 'download',
                err: err,
            });
        });
    console.log('read');
    // note read
    let stream = await bucket
        .file(object.name!)
        .createReadStream({
            start: 10000,
            end: 20000,
        })
        .on('error', function (err) {
            console.log('error 1');
            console.log({ error: err });
        })
         await new Promise((resolve, reject) => {
            console.log('error 2');
            stream.on('finish', resolve);
            console.log('error 3');
            stream.on('error', reject);
            console.log("end!")
            stream.on('end', resolve);
        }).catch((error) => {
            // successMessage is whatever we passed in the resolve(...) function above.
            // It doesn't have to be a string, but if it is only a succeed message, it probably will be.
            console.log("oups! " + error)
        });

    console.log('tempFile size2', fs.statSync(tempFilePath).size);­­­

    return fs.unlinkSync(tempFilePath);
});

and I got that error:

Function execution took 60008 ms, finished with status: 'timeout'


Solution

  • As the error message shows, the regular file system on Cloud Functions is read only. The only place you can write to is /tmp, as also shown in the documentation on file system access in Cloud Functions. I'm not sure why os.tmpdir() doesn't give you a location at that path, but you might want to hard-code the directory.

    One thing to keep in mind: /tmp is a RAM disk and not a physical disk, so your allocated memory will need to have enough space for the files you write to it.