[英]Can't Download File From Cloud Storage to Cloud Function Temporary Storage to Read and Process CSV Import
I am trying to use a firebase onCall funtion to download a CSV file from Cloud Storage, place it in cloud function temporary storage, then read it and upload it into a firestore collection called test. 我正在尝试使用Firebase onCall函数从Cloud Storage中下载CSV文件,将其放在云功能临时存储中,然后阅读并将其上传到名为test的Firestore集合中。 Unfortunately, I can't get the download to cloud function temporary storage to work.
不幸的是,我无法下载到云功能的临时存储。
I have tried to follow the google documentation at https://firebase.google.com/docs/storage/extend-with-functions and other examples with the "return bucket.file(filePath).download({destination: tempFilePath}), but without luck. 我尝试按照https://firebase.google.com/docs/storage/extend-with-functions以及其他示例中的google文档使用“ return bucket.file(filePath).download({destination:tempFilePath}) ,但没有运气。
Here is my code 这是我的代码
//THIS IS THE FUNCTION FOR LOADING AN PROCESSING A INCOMING CSV FILE
exports.uploadCSV = functions.https.onCall((data,context)=> {
//get the file name from the boday
const fileOne = data.id + '.csv';
console.log('fileOne', fileOne);
//bring together the director and file name
const fileName = 'uploads/' + fileOne;
console.log('fileName', fileName);
//setup the temporary path
const tmpFilePath = path.join(os.tmpdir(), fileName);
console.log('tmpFilePath', tmpFilePath);
//assign the Firbase project id
const projectId = 'xxxxxxxxx';
console.log('projectId')
//assign storage to the project
const storage = new Storage({ projectId: projectId });
console.log('new Storage')
//setup the bucket configuration
const bucket = admin.storage().bucket();
console.log('bucket', bucket)
//Reference Storage Bucket in firestore
const bucketName = 'xxxxxxxxx.appspot.com';
console.log('bucketName');
//sets myFile to value of file downloaded into cloud function temporary storage.
return storage.bucket(bucketName).file(fileName).download({destination: tmpFilePath})
.then(() => {
console.log('image downloaded to:', tmpFilePath)
//read stream and process
fs.createReadStream('tmpFilePath')
.pipe(csv())
.on('data', (row) => {
console.log(row);
if(row) {
firestore.collection('test').add({
name: row.Name
});
} else {console.log('no data')
}
})
.on('end', () => {
console.log('CSV file successfully processed.');
});
})
.then(() => {
//removes temporary directory
return fs.unlink(tmpFilePath);
})
.catch((err)=>{ console.log(err); });
});
While the Network console returns a 200, this is the error I receive in my firebase-functions logs. 网络控制台返回200时,这是我在Firebase功能日志中收到的错误。
{ Error: ENOENT: no such file or directory, open '/tmp/uploads/somedata.csv' errno: -2, code: 'ENOENT', syscall: 'open', path: '/tmp/uploads/somedata.csv' } {错误:ENOENT:没有这样的文件或目录,打开'/tmp/uploads/somedata.csv'errno:-2,代码:'ENOENT',syscall:'open',路径:'/tmp/uploads/somedata.csv '}
Your download path includes a folder called "uploads", but you never create that directory explicitly. 您的下载路径包括一个名为“上载”的文件夹,但是您从未明确创建该目录。 Instead, try just downloading to os.tmpdir() without any intermediate directories to create:
相反,请尝试仅下载到os.tmpdir()而不创建任何中间目录:
// no "uploads" here
const fileName = fileOne;
console.log('fileName', fileName);
const tmpFilePath = path.join(os.tmpdir(), fileName);
console.log('tmpFilePath', tmpFilePath);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.