简体   繁体   中英

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在此处输入图片说明

How can I do that?

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

ie 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);
  }
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM