Google Cloud Storage Triggers

Cloud Functions can respond to Object Change Notifications emerging from Google Cloud Storage. These change notifications are triggered in response to object addition (create), update (modify), or deletion.

The following gcloud command deploys a function that will be triggered by Cloud Storage:

gcloud beta functions deploy helloWorld --stage-bucket cloud-functions --trigger-bucket my-bucket
Argument Description
--trigger-bucket <name> The name of the Cloud Storage bucket the function will watch for changes.

Cloud Storage Events

Cloud Functions triggered from Cloud Storage events will be sent an event containing an Objects resource representing the object in Cloud Storage that was created, updated or deleted. The format of the change notification is as per the published format for Objects.

Cloud Storage events will be triggered whenever an object is created, updated or deleted on a Cloud Storage bucket. To make it easy to distinguish in the Cloud Functions case, we've also included the ResourceState property (usually sent as an HTTP header) in the body of the change notification in a property called resourceState which will have the value exists for object additions and updates, or not_exists for object deletions.

For example:

Node.js

/**
 * Background Cloud Function to be triggered by Cloud Storage.
 *
 * @param {object} event The Cloud Functions event.
 * @param {function} The callback function.
 */
exports.helloGCS = function helloGCS (event, callback) {
  const file = event.data;
  const isDelete = file.resourceState === 'not_exists';

  if (isDelete) {
    console.log(`File ${file.name} deleted.`);
  } else {
    console.log(`File ${file.name} uploaded.`);
  }

  callback();
};

See the Cloud Storage Tutorial for an example of how to implement a background function that is triggered by Cloud Storage.

Send feedback about...

Cloud Functions Documentation