Skip to content

Instantly share code, notes, and snippets.

@polleyg
Last active December 15, 2017 11:31
Show Gist options
  • Save polleyg/60695eba1a1a885655a021d27474bca0 to your computer and use it in GitHub Desktop.
Save polleyg/60695eba1a1a885655a021d27474bca0 to your computer and use it in GitHub Desktop.
Triggering Cloud Functions on folders in GCS

Cloud Functions are an extremely powerful and elegant way to solve problems. You don't need to worry about any of the infrastructure, nor trying to scale. It's a win-win! However, when deploying[1] your Cloud Function to trigger with GCS, you can currently only set the --trigger-bucket parameter to be an actual GCS bucket.

But what if you only want to trigger on a files in a specific folder(s) within that bucket?

Not to fret! There's a little trick for this. In fact, the object/file name (i.e. the new file that's been uploaded to your folder) actually contains the full path, including any folder names. So, in your Cloud Function all you need to do is this:

function(event, callback) {
const file = event.data;
if (file.resourceState === 'exists' && file.name && file.name.indexOf('my_lovely_folder/') !== -1) {
   //do stuff
   ..

[1] https://cloud.google.com/functions/docs/calling/storage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment