简体   繁体   English

如何从云功能访问 firebase 存储中的特定文件夹?

[英]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.我是第一次使用 firebase 云函数存储。 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}'即您不能'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);
  }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Firebase 云 Function 中删除 Firebase 存储文件夹? - How to delete a Firebase Storage folder from a Firebase Cloud Function? 如何使用函数从 Firebase 存储中删除文件夹? - How do I delete a folder from Firebase Storage using a function? 如何使用Firebase云功能存储创建文件夹 - How can make folder with Firebase Cloud Functions Storage 如何使用 Cloud Function 从 Cloud Storage 获取 Firebase 的 object? - How to get object from Cloud Storage for Firebase using Cloud Function? Cloud Storage for Firebase 访问错误“admin.storage(...).ref is not a function” - Cloud Storage for Firebase access error “admin.storage(…).ref is not a function” 从Cloud功能的Firebase存储下载时出错 - Error downloading from firebase Storage in a Cloud Function 如何使用云功能制作缩略图并将其放置在Firebase存储中的特定文件夹中? - how to make thumbnail and place it in the particular folder in Firebase storage using cloud function? 如何从云功能通过 Firebase 云消息传递发送通知? - How can I send a Notification via Firebase Cloud Messaging from a cloud function? Firebase Cloud Function,onCreate事件触发后如何访问父数据? - Firebase Cloud Function, how can I access parent data after onCreate event triggered? 如何在云端功能中查询Firebase数据库? - How do I query a firebase database inside a cloud function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM