如何通过云功能访问Firebase存储内的特定文件夹?

时间:2018-12-30 10:20:19

标签: node.js firebase google-cloud-functions firebase-storage

我第一次使用Firebase云功能存储。  我成功了 使用以下命令对默认文件夹执行更改:

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

但是现在我希望从存储中的文件夹中触发该功能 像这样的文件夹 enter image description here

我该怎么做?

1 个答案:

答案 0 :(得分:1)

当前无法为某些文件路径配置触发条件,类似于对数据库触发器的配置。

即您无法'User_Pictures/{path}'

设置云存储触发器

您需要做的是一旦触发该功能就检查the object attributes并在那里进行相应的处理。

针对每种情况要创建一个触发函数,如果不是您要查找的路径,则停止该函数。

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

  // ...
})

或者您执行主处理功能,将处理分派到不同的功能

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