Google云端功能 - 无法读取属性&get 39lication getfaultDefault'

时间:2018-03-18 13:07:24

标签: google-cloud-functions google-cloud-dataflow google-api-nodejs-client

我已经部署了一个云功能来调用数据流管道模板,并尝试通过将文件放在云存储桶中来触发该功能。

作为node.js的先决条件,我已经完成了,

npm init
npm install --save googleapis

Index.js

const google = require('googleapis');

exports.goWithTheDataFlow = function(event, callback) {
 const file = event.data;



   google.auth.getApplicationDefault(function (err, authClient, projectId) {
     if (err) {
       throw err;
     }

 console.log(projectId);
     if (authClient.createScopedRequired && authClient.createScopedRequired()) {
       authClient = authClient.createScoped([
         'https://www.googleapis.com/auth/cloud-platform',
         'https://www.googleapis.com/auth/userinfo.email'
       ]);
     }

     const dataflow = google.dataflow({ version: 'v1b3', auth: authClient });
            console.log(`gs://${file.bucket}/${file.name}`);
     dataflow.projects.templates.create({
       projectId: projectId,
       resource: {
         parameters: {
           inputFile: `gs://${file.bucket}/${file.name}`

         },
         jobName: 'cloud-fn-dataflow-test',
         gcsPath: 'gs://jaison/templates/ApacheBeamTemplate'
       }
     }, function(err, response) {
       if (err) {
         console.error("problem running dataflow template, error was: ", err);
       }
       console.log("Dataflow template response: ", response);
       callback();
     });

   });

 callback();
};

用于部署云功能的命令:

gcloud beta functions deploy goWithTheDataFlow --stage-bucket cf100stage --trigger-bucket cf100

数据流(Apache beam): 我能够从控制台执行数据流模板,下面是模板的路径,

'gs://jaison/templates/ApacheBeamTemplate'

功能因以下错误而崩溃:

  

TypeError:无法读取属性' getApplicationDefault'未定义的   在exports.goWithTheDataFlow(/user_code/index.js:11:17)at   /var/tmp/worker/worker.js:695:16 at /var/tmp/worker/worker.js:660:9 at   _combinedTickCallback(internal / process / next_tick.js:73:7)at process._tickDomainCallback(internal / process / next_tick.js:128:9)

看起来我缺少图书馆。不知道如何解决这个问题。请帮忙。

1 个答案:

答案 0 :(得分:2)

我的云功能适用于以下更改,

1.将GOOGLE_APPLICATION_CREDENTIALS设置为服务帐户json文件

join

2.index.js

export GOOGLE_APPLICATION_CREDENTIALS="/path/of/svc/json/file.json"