when i am running the gcloud functions deploy gcp_test --trigger-resource xxx-test-123 --trigger-event google.storage.object.finalize i am getting syntax error .
ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Function load error: Code in file index.js can't be loaded. Is there a syntax error in your code? Detailed stack trace: /user_code/index.js:1 (function (exports, require, module, __filename, __dirname) { # index.js ^
SyntaxError: Invalid or unexpected token at createScript (vm.js:56:10) at Object.runInThisContext (vm.js:97:10) at Module._compile (module.js:549:28) at Object.Module._extensions..js (module.js:586:10) at Module.load (module.js:494:32) at tryModuleLoad (module.js:453:12) at Function.Module._load (module.js:445:3) at Module.require (module.js:504:17) at require (internal/module.js:20:19) at getUserFunction (/var/tmp/worker/worker.js:388:24)
I am not sure why this error is appearing. I have installed npm module for @google-cloud/storage . Any help is appreciated .This function is suppose to enable stackdriver logging . reference: 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);
});
}