javascript - 错误: ENOENT: no such file or directory even when file exists in Firebase Cloud Functions

标签 javascript node.js firebase google-cloud-functions fs

我对 Cloud Functions 比较陌生,并且已经尝试解决此问题一段时间了。本质上,每当完整上传到 Firebase Cloud Storage 时,就会调用我尝试编写的函数。然而,当函数运行时,有一半的时间,它运行到以下错误:

The following error occured:  { Error: ENOENT: no such file or directory, open '/tmp/dataprocessing/thisisthefilethatiswritten.zip'
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '/tmp/dataprocessing/thisisthefilethatiswritten.zip' }

代码如下:

const functions = require('firebase-functions');
const admin = require('firebase-admin')
const inspect = require('util').inspect
const path = require('path');
const os = require('os');
const fs = require('fs-extra');

const firestore = admin.firestore()
const storage = admin.storage()

const runtimeOpts = {
    timeoutSeconds: 540,
    memory: '2GB'
  }

const uploadprocessing = functions.runWith(runtimeOpts).storage.object().onFinalize(async (object) => {
    const filePath = object.name
    const fileBucket = object.bucket
    const bucket_fileName = path.basename(filePath);
    const uid = bucket_fileName.match('.+?(?=_)')
    const original_filename = bucket_fileName.split('_').pop()

    const bucket = storage.bucket(fileBucket);
    const workingDir = path.join(os.tmpdir(), 'dataprocessing/');
    const tempFilePath = path.join(workingDir, original_filename);

    await fs.ensureDir(workingDir)

    await bucket.file(filePath).download({destination: tempFilePath})

    //this particular code block I included because I was worried that the file wasn't
    //being uploaded to the tmp directly, but the results of the function 
    //seems to confirm to me that the file does exist.

    await fs.ensureFile(tempFilePath)
        console.log('success!')
        fs.readdirSync(workingDir).forEach(file => {
            console.log('file found: ', file);
            });
        console.log('post success')
        fs.readdirSync('/tmp/dataprocessing').forEach(file => {
            console.log('tmp file found: ', file);
    })


    fs.readFile(tempFilePath, function (err, buffer) {
        if (!err) {
            //data processing comes here. Please note that half the time it never gets into this
            //loop as instead it goes into the else function below and outputs that error.
        }
        else {
            console.log("The following error occured: ", err);
        }
    })
    fs.unlinkSync(tempFilePath);
    return
})
module.exports = uploadprocessing;

我一直在尝试很多不同的事情,奇怪的是,当我将代码添加到“if (!err)”(由于错误而实际上并未运行)时,它有时会任意开始工作一致,但是当我添加不同的代码时它会停止工作。我本以为问题是由我添加的代码引起的,但是当我只是更改/添加/删除注释时,错误就出现了...从技术上讲,这应该对运行的函数没有影响...

有什么想法吗?先感谢您!!! :)

最佳答案

fs.readFile 是异步的并立即返回。一段时间后将使用缓冲区的内容调用您的回调函数。这意味着 fs.unlinkSync 将在读取文件的同时删除该文件。这意味着您实际上存在竞争条件,并且文件可能会在读取之前被删除。

您的代码应等到读取完成后再继续删除。也许您想使用 fs.readFileSync相反。

关于javascript - 错误: ENOENT: no such file or directory even when file exists in Firebase Cloud Functions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60984659/

相关文章:

ios - Firebase 给了我 Use of undeclared type with FIRDatabase and FIRDataSnapshot

javascript - 如何使用 Javascript 读取获取请求?

javascript - 如何使用 setInterval() 作为事件监听器

node.js - 无法使用 Express js 发布表单详细信息

node.js - 在heroku上部署后无法读取mongo数据库,返回503错误,但在本地可以工作

ios - 如何使用 Firestore 中的对象将数据设置为数组

java - 从 Firebase 数据库读取特定数据

php - iframe 的 SEO 友好替代方案?

javascript - 使用 ComponentFactoryResolver 渲染 Angular Material 组件

html - 如何在使用更改的 html 文件时保留套接字 io ID?