无法将文件从云存储下载到云功能临时存储以读取和处理CSV导入

时间:2019-04-09 22:46:12

标签: node.js firebase google-cloud-platform google-cloud-storage google-cloud-functions

我正在尝试使用Firebase onCall函数从Cloud Storage中下载CSV文件,将其放置在Cloud函数临时存储中,然后阅读并将其上传到名为test的Firestore集合中。不幸的是,我无法下载到云功能的临时存储。

我尝试遵循https://firebase.google.com/docs/storage/extend-with-functions和其他示例中的google文档,并带有“ return bucket.file(filePath).download({destination:tempFilePath}”),但是没有运气。

这是我的代码

//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); });

});

网络控制台返回200时,这是我在firebase功能日志中收到的错误。

{错误:ENOENT:没有这样的文件或目录,请打开'/tmp/uploads/somedata.csv'   errno:-2,   代码:“ ENOENT”,   syscall:“打开”,   路径:'/tmp/uploads/somedata.csv'}

1 个答案:

答案 0 :(得分:1)

您的下载路径包括一个名为“ uploads”的文件夹,但是您永远不会明确创建该目录。相反,请尝试仅下载到os.tmpdir()而无需创建任何中间目录:

// no "uploads" here
const fileName = fileOne;
    console.log('fileName', fileName);

const tmpFilePath = path.join(os.tmpdir(), fileName);
    console.log('tmpFilePath', tmpFilePath);