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

怎甘沉沦 提交于 2021-02-18 08:40:08

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/53976808/how-can-i-access-a-specific-folder-inside-firebase-storage-from-a-cloud-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!