简体   繁体   English

firebase 云函数 onFinalize 触发器没有预定义的存储桶?

[英]firebase cloud functions onFinalize trigger with not predefined bucket?

Is it possible to write a firebase cloud function that triggers when a new fila was created in firebase storage - onFinalize (but we don't know the exact bucket in advance) ?是否可以编写一个在 firebase 存储中创建新 fila 时触发的 firebase 云函数 - onFinalize(但我们事先不知道确切的存储桶)?

Inside my firebase storage a have a folder 'loads' and inside I have folders named with load id like:在我的 firebase 存储中,有一个文件夹“loads”,在我的文件夹中,我有一个以 load id 命名的文件夹,例如:

  • /loads/-Lw1UySdunYyFMrrV6tl /loads/-Lw1UySdunYyFMrrV6tl
  • /loads/-LwisA5hkNl_uxw3k36f /加载/-LwisA5hkNl_uxw3k36f
  • /loads/-LwkPUm-q7wNv-wZ49Un /加载/-LwkPUm-q7wNv-wZ49Un

https://ibb.co/NnQkTyC here's a screenshot of storage https://ibb.co/NnQkTyC这是存储的屏幕截图

I want to trigger cloud function when new file has been created inside one of these folders.我想在这些文件夹之一中创建新文件时触发云功能。 I don't know in andvance where the file will be created.我不知道在哪里创建文件。 I don't know if this is even possible.我不知道这是否可能。 That's why I need an advice.这就是为什么我需要一个建议。

My main goal is to merge 2 pdf files in one within cloud functions.我的主要目标是在云功能中将 2 个 pdf 文件合并为一个。 In my app (TMS written with vuejs) on frontend I create confirmationOrder.pdf using html2canvas/jsPDF and then save it to storage/loads/${loadID}.在前端的应用程序(用 vuejs 编写的 TMS)中,我使用 html2canvas/jsPDF 创建了 ConfirmationOrder.pdf,然后将其保存到 storage/loads/${loadID}。 And later on user can manually upload POD.pdf on the same bucket.稍后用户可以在同一个存储桶上手动上传 POD.pdf。 When it happens I want my cloud function to merge these two pdfs in one new file (in same storage bucket).发生这种情况时,我希望我的云功能将这两个 pdf 合并到一个新文件中(在同一个存储桶中)。 But again I don't know the bucket in advance.但我又一次不知道水桶。

Here's how I upload PDFs in frontend:这是我在前端上传 PDF 的方法:

 async uploadConfPDF({ commit }, payload) {
      const PDF = payload.PDF;
      const loadID = payload.loadID;
      const fileName = payload.fileName;

      const fileData = await firebase
        .storage()
        .ref(`loads/${loadID}/${fileName}.pdf`)
        .put(PDF);

      const confOrderURL = await fileData.ref.getDownloadURL();

      return confOrderURL;
    }, 

Any help is highly appreciated.任何帮助都受到高度赞赏。 Sorry if my explanation could seem not clear enough.对不起,如果我的解释似乎不够清楚。 English is not my native language.英语不是我的母语。

EDIT FOLLOWING YOUR QUESTION RE-WORKING根据您的问题进行编辑

Based on your code and on the print screen of your Cloud Storage console, you are working in the default bucket of your project, which location's URL is gs://alex-logistics.appspot.com .根据您的代码和 Cloud Storage 控制台的打印屏幕,您正在项目的默认存储gs://alex-logistics.appspot.com工作,该位置的 URL 为gs://alex-logistics.appspot.com

As we can see on the print screen of your Cloud Storage console, the files in your bucket are presented in a hierarchical structure , just like the file system on your local hard disk.正如我们在 Cloud Storage 控制台的打印屏幕上看到的那样,存储桶中的文件以分层结构呈现,就像本地硬盘上的文件系统一样。 However, this is just a way of presenting the files: there aren't genuine folders/directories in a bucket, the Cloud Storage console just uses the different part of the files paths to "simulate" a folder structure.然而,这只是呈现文件的一种方式:存储桶中没有真正的文件夹/目录,云存储控制台只是使用文件路径的不同部分来“模拟”文件夹结构。


So, based on the above paragraphs, I think that your question can be re-phrased to " In a Cloud Function, how can I extract the different parts of the path of a file that is uploaded to the default bucket of my Firebase Project? ".因此,基于以上段落,我认为您的问题可以重新表述为“在云函数中,如何提取上传到我的 Firebase 项目默认存储桶的文件路径的不同部分? ”。

Answer:回答:

In a Cloud Function that is triggered when a file is added to the default bucket, you can get the file path as follows:在将文件添加到默认存储桶时触发的 Cloud Function 中,您可以通过以下方式获取文件路径:

exports.yourCloudFunction = functions.storage.object().onFinalize(async (object) => {

    const filePath = object.name; // File path in the bucket.

    //...

});

Since we use an onFinalize event handler, you get the path of this new file by using the name property of the object Object, which is of type ObjectMetadata .由于我们使用onFinalize事件处理程序,因此您可以通过使用object Object 的name属性来获取此新文件的路径,该object的类型为ObjectMetadata

You can then use some String methods to, for example, extract from this path the ${loadID} you refer to in your question.然后,您可以使用一些String方法,例如,从此路径中提取您在问题中引用的${loadID}

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

相关问题 Firebase 的云功能是否准时触发? - Cloud Functions for Firebase trigger on time? 在 onFinalize 触发的 firebase 云函数中获取创建文件的兄弟姐妹 - Get siblings of created file in firebase cloud function triggered by onFinalize Firebase云功能数据库触发'onCreate不是函数' - Firebase Cloud Functions Database Trigger 'onCreate is not a function' Firebase Cloud Database触发器功能并非总是能完成 - Firebase Cloud Database Trigger Functions not always completing Firebase“创建”触发器的云函数 - 多个 collections - Cloud Functions for Firebase “Create” Trigger - multiple collections Firebase Firestore云功能显示触发器类型“未知” - Firebase firestore cloud functions showing trigger type 'unknown' Firebase onWrite触发器的云函数:snapshot.val不是函数 - Cloud Functions for Firebase onWrite trigger: snapshot.val is not a function 在Google Cloud Functions中更新Firebase更改触发器中的参考 - Updating a ref in a change Firebase trigger in Google Cloud Functions 是否可以根据用户的时区触发Firebase中的云功能? - Is it possible to trigger cloud functions in Firebase based on the user's time zone? 从 Cloud Functions for Firebase 中的数据库触发器获取用户 ID? - Getting the user id from a database trigger in Cloud Functions for Firebase?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM