Amr Mahmoud
Amr Mahmoud

Reputation: 122

How can I access a specific folder inside firebase storage from a cloud function?

I am using firebase cloud function storage for the first time. I succeeded to perform changes on the default folder by using :

exports.onFileUpload = functions.storage.bucket().object().onFinalize(data => {
const bucket = data.bucket;
const filePath = data.name;
const destBucket = admin.storage().bucket(bucket);
const file = destBucket.file(filePath);

but now I want the function to be triggered from a folder inside the storage folder like this enter image description here

How can I do that?

Upvotes: 1

Views: 2688

Answers (1)

Dennis Alund
Dennis Alund

Reputation: 3159

There is currently no way to configure trigger conditions for certain file paths, similar to what you can do with database triggers.

i.e. you can not set a cloud storage trigger for 'User_Pictures/{path}'

What you have to do is to inspect the object attributes once the function is triggered and handle it accordingly there.

Either you create a trigger function for each case you want to handle and stop the function if it's not the path you're looking for.

functions.storage.object().onFinalize((object) => {
  if (!object.name.startsWith('User_Pictures/')) {
    console.log(`File ${object.name} is not a user picture. Ignoring it.`);
    return null;
  }

  // ...
})

Or you do a master handling function that dispatches the processing to different functions

functions.storage.object().onFinalize((object) => {
  if (object.name.startsWith('User_Pictures/')) {    
    return handleUserPictures(object);
  } else if (object.name.startsWith('MainCategoryPics/')) {
    return handleMainCategoryPictures(object);
  }
})

Upvotes: 4

Related Questions