Is it possible to scope Firebase functions to a folder inside a storage bucket?
Asked Answered
W

4

21

I have tested Firebase functions for storage successfully. However, I havn't seen anywhere a hint how to only invoke the function when a file is added into a folder inside my bucket. The only hint I have seen about scoping the function is for different buckets here.

Is it possible to scope the function to a folder inside my bucket , if yes how? Or would I need to have multiple buckets instead of folders to separate different tasks.

Wham answered 30/7, 2017 at 20:15 Comment(0)
M
26

firebaser here

There is currently no way to trigger Cloud Functions only for writes in a specific folder in Cloud Storage. If you want to limit the triggering to a subset of the files in your project, putting them in a separate bucket is currently the only way to accomplish that.

As a workaround, you can write metadata about the image to a supported database (Realtime Database or Cloud Firestore), and use that to trigger a Cloud Function that transforms the file. This is what I usually do, as it also allows me to capture the metadata in a format that can be queried.

Midsection answered 30/7, 2017 at 22:31 Comment(6)
Thanks Frank, that settles it. I hope though that in the future this would be possible as it makes things a little bit more easier.Wham
Maybe you could do a work around by listening to anything created in Storage, then with the reference of that new file create a database node, then, a listener for only some of those nodesSturges
Is it still the only way? In this way the function is triggered even when not needed!Xanthe
This is bad. It basically results in unnecessary calls and therefore possible charges for nothing...Elytron
Since this was raised, path patterns are now available and should be the solution.Trattoria
@Trattoria Path pattern are not meant to used in setting bucket namesUnpolite
M
5

You may check inside the function. Get the "filePath" or "fileDir" on your function and check if it is the folder you want.

const path = require('path');
const filePath = event.data.name;
const fileDir = path.dirname(filePath);

//if you want to check the posts folder only then:

if (fileDir != 'posts') {
    console.log('This is not in post folder');
    return null;
}
Mistaken answered 24/11, 2017 at 18:24 Comment(1)
Thanks for the reply but I am trying to avoid triggering my function when it is not needed.Wham
S
5

Please note that Google Cloud Storage works on a flat filesystem. So practically there are no directories. If you're storing a file like /users/profile_pictures/photo.jpg it is basically all part of the file name. So in reality, there are no directories. There are just files. Which is why there cannot be a trigger on a directory per se. Of course you can work that around by checking the name of the file itself and see whether its start matches a particular string or not.

export const generateThumbnailTrigger = functions.storage.object().onFinalize(async (object) => {
    const filePath = object.name;
    if (filePath?.startsWith('temp/')) {
        console.log('start doing something with the file');
    } else {
        return false;
    }
});
Sextant answered 8/7, 2020 at 8:23 Comment(0)
T
0

This is better late than never. You can trigger from the audit log.

You want to create something like the following:

gcloud eventarc triggers update <trigger-name> \
  --destination-run-service="<function name>" \
  --destination-run-region="<region of function>" \
  --location="<region of trigger and bucket>" \
  --event-filters="methodName=storage.objects.create" \
  --event-filters="resourceLocation=<region same as location>"\
  --event-filters="type=type=google.cloud.audit.log.v1.written"
  --event-filters-path-pattern="projects/_/buckets/<your bucket name>/objects/<your filter pattern>"

Further reading available here: https://cloud.google.com/eventarc/docs/path-patterns

Not all services offer the path-pattern filtering, so you need to run something like this to get the details: gcloud eventarc providers describe cloudaudit.googleapis.com

I hope that helps, 6 years later :D

Feel free to update to relate to firebase more specifically or mark as answered.

Trattoria answered 21/9, 2023 at 5:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.