Skip to Content

Triggering Cloud Functions on folders in Google Cloud Storage

Beyond bucket related events, react upon individual files and folders

Posted on One min read

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 your Cloud Function to trigger with Google Cloud Storage, 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 file or folder in a specific folder 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 checking if the path includes your folder name:

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