Firebase Google Cloud Function:createReadStream结果为空文件 [英] Firebase Google Cloud Function: createReadStream results in empty file

查看:160
本文介绍了Firebase Google Cloud Function:createReadStream结果为空文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过Google Cloud Function处理视频文件(存储在Google Firebase存储中).我的工作代码将整个视频文件下载到NodeJS Google云功能中: await bucket.file(filePath).download({destination:tempFile}).但是目标只是读取帧速率,因此视频文件的标头就足够了.但是 createReadStream 给了我一个空的tempFile.任何建议,不胜感激!

I try to process a Video file (stored in Google Firebase storage) through a Google Cloud Function. I have working code that download the entire video files into the NodeJS Google cloud function: await bucket.file(filePath).download({ destination: tempFile }). But the goal is only to read the framerate, therefore the headers of the videofile would suffice. But createReadStream gives me an empty tempFile. Any advise much appreciated!

exports.checkFramerate = functions.region('europe-west1').storage.object().onFinalize(async (object, context) => {
    const bucket =  admin.storage().bucket(object.bucket); // Bucket class
    const filePath = object.name;   //  videos/xbEXdMNFb1Blbd9r2E8m/comp_test.mp4
    const fileName = filePath.split('/').pop(); // comp_test.mp4
    const bucketDir = path.dirname(filePath); // videos/xbEXdMNFb1Blbd9r2E8m

    const tempFile = path.join(os.tmpdir(), 'temp.mp4')
    fs.closeSync(fs.openSync(tempFile, 'w'))
    console.log("tempFile size1", fs.statSync(tempFile).size)

    // await bucket.file(filePath).download({ destination: tempFile }); // this works: tempFile size2 = 3180152
    await bucket.file(filePath).createReadStream({                      // this does not work: tempFile size2 = 0
        start: 10000,
        end: 20000
      })
      .on('error', function(err) {console.log(err)})
      .pipe(fs.createWriteStream(tempFile));


    console.log("tempFile size2", fs.statSync(tempFile).size)

    mi(tempFile).then(data => {
        console.log("frameRate", data[0].general.frame_rate[0])
        return data[0].general.frame_rate[0];
    }).catch(e => {console.error(e)});
});

我甚至尝试实现了 https://googleapis.dev/nodejs/storage/latest/File.html#createReadStream ,但无济于事.remoteFile.download的工作非常漂亮,但是remoteFile.createReadStream给了我空文件...

I tried implementing even the example of https://googleapis.dev/nodejs/storage/latest/File.html#createReadStream but to no avail. remoteFile.download works beautifully but remoteFile.createReadStream gives me empty files...

const remoteFile = bucket.file(filePath);
const localFilename = tempFile;

remoteFile.createReadStream()
    .on('error', function(err) {})
    .on('response', function(response) {})
    .on('end', function() {})
    .pipe(fs.createWriteStream(localFilename));

fs.stat(localFilename, (err, stats) => {
    if (err) {console.log(err)}
    return console.log("stats async",stats.size)
})

推荐答案

您的问题是没有提供流API.因此, await 不会执行任何操作,并且您的函数会在流传输之前继续运行,并且当您第二次将其设置为Status时,该文件的长度仍然为零.

Your problem is that the stream API isn't promisifed. So, the await does nothing, and your function continues before the stream is piped, and the file is still zero-length when you stat it the second time.

download 方法可以正常工作,因为它返回了Promise.

The download method works just fine because it returns a Promise.

此答案概述了您需要采用的一般方法.总而言之,您基本上希望代码的执行管道部分如下所示:

This answer outlines the general approach you need to take. In summary though, you basically want the section of your code that does the piping to read like this:

const stream = bucket.file(filePath).createReadStream({
    start: 10000,
    end: 20000
  })
  .pipe(fs.createWriteStream(tempFile));

await new Promise((resolve, reject) => {
    stream.on('finish', resolve);
    stream.on('error', reject);
  });

console.log("tempFile size2", fs.statSync(tempFile).size)

然后,您的函数将等待,直到管道完成且流关闭时发生 finish 事件为止.显然,您可能也想对错误处理程序做一些更聪明的事情,但这是您所需要的一般形式.

Your function will then wait until the finish event occurs when the piping is complete and the stream is closed. Obviously you probably want to do something more clever with the error handler too, but this is the general form of what you need.

这篇关于Firebase Google Cloud Function:createReadStream结果为空文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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