谷歌云功能 - 无法读取属性“getApplicationDefault" [英] Google cloud functions - cannot read property 'getApplicationDefault'

查看:21
本文介绍了谷歌云功能 - 无法读取属性“getApplicationDefault"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have deployed a cloud function to invoke a dataflow pipeline template and trying to trigger the function by placing the file in cloud storage bucket.

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

As node.js prerequisite I have done,

npm init
npm install --save googleapis

索引.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();
};

用于部署云功能的命令:

Command used to deploy cloud function:

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

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

Dataflow(Apache beam): I was able to execute the dataflow template from console and below is the path of the template,

'gs://jaison/templates/ApacheBeamTemplate'

函数崩溃并出现以下错误:

Function crashes with below error:

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

TypeError: Cannot read property 'getApplicationDefault' of undefined at 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)

看起来我缺少库.不知道如何解决这个问题.请帮忙.

Looks like I am missing libraries. Not sure how to fix this. Please help.

推荐答案

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

My cloud function works with below changes,

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

1.Setting up GOOGLE_APPLICATION_CREDENTIALS to service account json file

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

2.index.js

var {google} = require('googleapis');

exports.TriggerBeam = (event, callback) => {


const file = event.data;
  const context = event.context;

  console.log(`Event ${context.eventId}`);
  console.log(`  Event Type: ${context.eventType}`);
  console.log(`  Bucket: ${file.bucket}`);
  console.log(`  File: ${file.name}`);
  console.log(`  Metageneration: ${file.metageneration}`);
  console.log(`  Created: ${file.timeCreated}`);
  console.log(`  Updated: ${file.updated}`);

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

 console.log(projectId);

 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-beam-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();
};

这篇关于谷歌云功能 - 无法读取属性“getApplicationDefault"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆