我对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)”(由于错误而实际上未运行)时,它有时会以任意一致的方式开始工作,但是当我添加其他代码时它停止工作。我本来以为这个问题是由我添加的代码引起的,但是当我只是更改/添加/删除注释时,也会出现字面上的错误...从技术上讲,这应该对运行的函数没有影响...] >
有什么想法吗?先感谢您!!! :)
我对Cloud Functions相对较新,并且已经尝试解决此问题已有一段时间了。本质上,每当有完整的上传到Firebase时,就会调用我要编写的函数...
fs.readFile
是异步