Cloud functions triggered by GCS object notifications

Similarly, Cloud Functions can also be triggered by changes in Cloud Storage buckets:

  1. Write the Cloud Function and save it as index.js:
/**
* Background Cloud Function to be triggered by Cloud Storage..
*/
exports.helloGCS = function (event, callback)
{
const file = event.data;

if (file.resourceState === 'not_exists')
{
console.log(`File ${file.name} deleted.`);
}
else if (file.metageneration === '1')
{
// metageneration attribute is updated on metadata changes.
// on create value is 1
console.log(`File ${file.name} uploaded.`);
}
else
{
console.log(`File ${file.name} metadata updated.`);
}

callback();
};
  1. Deploy the Cloud Function:
gcloud beta functions deploy helloGCS --trigger-bucket <BUCKET_NAME>  
  1. Trigger (invoke) the Cloud Function
  2. As before, we can invoke this function either by uploading a file (say test.txt) to the bucket, or through an equivalent direct invocation:
gcloud beta functions call helloGCS --data '{"name":"test.txt"}'

Once invoked, the logs should contain the log entry stating that test.txt file was uploaded to the GCS bucket.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.105.59