从云函数在文件中运行Python脚本

2024-09-24 08:33:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用python引擎运行。所以我用pythonshell编写了一个云函数代码。但是这个错误出现了。在

Error: python: can't open file '/home/dmproject0608/test2/FaceDetect_.py': [Errno 2] No such file or directory

这是我的云功能:

const functions = require('firebase-functions');
const mkdir=require('mkdirp-promise');
const gcs=require('@google-cloud/storage')();
const spawn=require('child-process-promise').spawn;
const path=require('path');
const os=require('os');
const fs=require('fs');
var pythonShell=require('python-shell');

exports.Test = functions.storage.object().onFinalize((object) => {
    const filePath = object.name;
    const fileName = path.basename(filePath, path.extname(filePath));
    const fileDir = path.dirname(filePath);
    const fileBucket=object.bucket;
    const tempLocalFile = path.join(os.tmpdir(), filePath);
    const tempLocalDir = path.dirname(tempLocalFile);
    const bucket=gcs.bucket(fileBucket);

    if (object.resourceState === 'not_exists') {
        console.log('This is a deletion event.');
        return;
    }

    return mkdir(tempLocalDir).then(()=>{
        return bucket.file(filePath).download({destination : tempLocalFile});
    }).then(()=>{
        console.log('Download is complete %j',tempLocalFile);
        var options={
            mode:'text',
            pythonPath:'',
            pythonOptions: ['-u'],
            scriptPath:'/home/dmproject0608/test2',
            args:[ tempLocalFile]
        };
        pythonShell.run('FaceDetect_.py',options,function(err,results){
            if(err) throw err;
            console.log('result: %j',results);
        });
    });
});

Tags: pathloghomereturnobjectbucketosrequire