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
..