【问题标题】:Google cloud function deploy fails谷歌云功能部署失败
【发布时间】:2019-01-30 15:07:13
【问题描述】:

当我运行 gcloud 函数时 deploy gcp_test --trigger-resource xxx-test-123 --trigger-event google.storage.object.finalize 我收到语法错误。

错误:(gcloud.functions.deploy)操作错误:代码=3, 消息=函数加载错误:文件 index.js 中的代码无法加载。是 您的代码中有语法错误吗?详细的堆栈跟踪: /user_code/index.js:1 (函数 (exports, require, module, __filename, __dirname) { # index.js ^

SyntaxError:无效或意外的令牌 在 createScript (vm.js:56:10) 在 Object.runInThisContext (vm.js:97:10) 在 Module._compile (module.js:549:28) 在 Object.Module._extensions..js (module.js:586:10) 在 Module.load (module.js:494:32) 在 tryModuleLoad (module.js:453:12) 在 Function.Module._load (module.js:445:3) 在 Module.require (module.js:504:17) 在需要(内部/module.js:20:19) 在 getUserFunction (/var/tmp/worker/worker.js:388:24)

我不确定为什么会出现此错误。我已经为 @google-cloud/storage 安装了 npm 模块。任何帮助表示赞赏。此功能假定启用堆栈驱动程序日志记录。 参考:https://medium.com/google-cloud/encrypting-stackdriver-logging-sinks-using-customer-managed-encryption-keys-for-gcs-ccd0b59f0a3

    # index.js
            'use strict';
            const Buffer = require('safe-buffer').Buffer;
            // Imports the Google Cloud client library
            const Storage = require('@google-cloud/storage');
            // Creates a client
            const storage = new Storage();
            exports.moveFileToEncryptedStorage = (event, callback) => {
              const file = event.data;
              console.log(  Event ${event.eventId});
              console.log(  Event Type: ${event.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});
            const newBucket = "xxx-test-123";
            const newBucketAndFileName = "gs://"+newBucket+"/"+file.name;
            console.log("Moving to..."+newBucketAndFileName);
            return storage
                  .bucket(file.bucket)
                  .file(file.name)
                  .move(newBucketAndFileName)
                  .then(() => {
            
                    console.log("content has been moved to "+newBucketAndFileName);
            })
                  .catch((err) => {
                    console.error("ERROR:"+err);
                  });
            }

【问题讨论】:

    标签: javascript google-cloud-platform google-cloud-stackdriver


    【解决方案1】:

    删除以下行 # index.js 就可以了。请记住,所有 console.log 行应如下所示:console.log(" Event ${event.eventId}");

    【讨论】:

      猜你喜欢
      • 2020-05-21
      • 1970-01-01
      • 2020-10-19
      • 2017-08-15
      • 2021-02-26
      • 2018-09-14
      • 2020-05-21
      • 1970-01-01
      • 2022-09-01
      相关资源
      最近更新 更多