Google云功能部署失败

时间:2018-08-24 07:16:58

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

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

  

错误:(gcloud.functions.deploy)OperationError:代码= 3,   message =函数加载错误:无法加载文件index.js中的代码。是   您的代码中有语法错误?详细的堆栈跟踪:   /user_code/index.js:1(函数(导出,要求,模块,__文件名,   __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)       在要求时(internal / module.js:20:19)       在getUserFunction(/var/tmp/worker/worker.js:388:24)

我不确定为什么会出现此错误。我已经为@ google-cloud / storage安装了npm模块。感谢您提供任何帮助。此功能是为了启用stackdriver logging。 参考: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);
                  });
            }

1 个答案:

答案 0 :(得分:1)

删除下面的行# index.js就可以了。请记住,所有console.log行应类似于以下内容:console.log(" Event ${event.eventId}");

相关问题