【问题标题】:Error while executing Dataflow template using cloud function使用云功能执行数据流模板时出错
【发布时间】:2018-07-12 18:34:20
【问题描述】:

尝试使用 Google Cloud 功能执行自定义数据流模板时出现以下错误。

Error:"problem running dataflow template, error was: { Error: A Forbidden error was returned while trying to retrieve an access token for the Compute Engine built-in service account. 这可能是因为 Compute Engine 实例没有指定了正确的权限范围。无法刷新访问令牌”。

我已尝试提供所有必需的权限和范围。请有人提出解决方案。

【问题讨论】:

    标签: node.js google-cloud-platform google-cloud-functions google-cloud-dataflow


    【解决方案1】:

    google-cloud 节点库尚不支持 Dataflow API,因此当前使用该 API 的方式是 googleapis 库。

    按照那里的说明,我尝试使用 HTTP 触发的函数使用 Google-provided 模板启动 Dataflow 作业,但没有遇到任何问题:

    const {google} = require('googleapis');
    const project = "your-project-id"
    
    exports.launchDataflowTemplate = (req, res) => {
        let result;
        google.auth.getApplicationDefault(function(err, authClient, projectId) {
                if (err) {
                    throw err;
                }
                if (authClient.createScopedRequired && authClient.createScopedRequired()) {
                    authClient = authClient.createScoped([
                        'https://www.googleapis.com/auth/cloud-platform',
                        'https://www.googleapis.com/auth/compute',
                        'https://www.googleapis.com/auth/compute.readonly',
                        'https://www.googleapis.com/auth/userinfo.email'
                    ]);
                }
                var dataflow = google.dataflow({
                    version: "v1b3",
                    auth: authClient
                });
    
                var launchParams = {
                    "inputFilePattern": "gs://your-input-bucket/*.gz",
                    "outputDirectory": "gs://your-result-bucket/",
                    "outputFailureFile": "gs://your-logs-bucket/error.csv"
                };
    
                var env = {
                   "tempLocation": "gs://your-staging-bucket/temp",
                   "zone": "us-central1-f"
                }
    
    
                var opts = {
                    projectId: project,
                    gcsPath: "gs://dataflow-templates/latest/Bulk_Decompress_GCS_Files",
                    resource: {
                        parameters: launchParams,
                        environment: env
                    }
                };
    
                dataflow.projects.templates.launch(opts, (err, result) => {
                    if (err) {
                        throw err;
                    }
                    res.send(result.data);
                });
        });
    };
    

    【讨论】:

    • 嗨@Jofre,我正在尝试您的代码,但由于“TypeError:无法读取未定义的属性'getApplicationDefault'”而失败。您使用了哪个 Node 和 googleapis 版本?
    • 你是在 GCF 中运行这个吗?这看起来像是图书馆收集凭据的问题。
    • 它适用于{"dependencies":{"googleapis" : "22.2.0"}},但适用于{"dependencies":{"googleapis" : "28.1.0"}}。在相关帖子中看起来与 this comment 相关。我再看远一点。
    • 是的,看起来他们对模块进行了一些更改。将要求更改为 const { google } = require('googleapis'); 修复了它
    • 按原样编写,版本24.0.0 是最后一个工作,25.0.0 失败。我稍后会尝试您的建议并相应地更新答案。感谢您提出这个问题!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-18
    • 2020-07-12
    • 1970-01-01
    • 1970-01-01
    • 2021-09-10
    • 2018-10-24
    • 2020-06-05
    相关资源
    最近更新 更多