简体   繁体   English

Firebase 特定文件上传时的云存储触发器

[英]Firebase Cloud Storage trigger on Specific File Upload

I'm trying to build the functionality that will automatically trigger an email through nodemailer when a specific file is uploaded to Firebase Storage.我正在尝试构建当特定文件上传到 Firebase 存储时通过 nodemailer 自动触发 email 的功能。 For flow - a user completes a form, the data is aggregated and a PDF is automatically generated, the PDF is then added to Cloud Storage.对于流 - 用户完成表单,数据被聚合并自动生成 PDF,然后将 PDF 添加到 Cloud Storage。

The Storage path is "UserFiles/{uID}/"(in here lives the user's file)". When a specific file is finalized in Storage (called "Resume.pdf"), I'd like to send all of the files in that uID folder. Is this possible with cloud functions? I've built functionality to manually trigger this if a user clicks a button, but I'd like the email to automatically be sent when the upload is complete.存储路径是“UserFiles/{uID}/”(这里是用户的文件)。当特定文件在存储中完成(称为“Resume.pdf”)时,我想将所有文件发送到那个 uID 文件夹。这可能与云功能有关吗?我已经构建了在用户单击按钮时手动触发此功能的功能,但我希望在上传完成时自动发送 email。

Here is the manual send (works fine):这是手动发送(工作正常):

  const getDocumentURLs = () => {
firebase
  .storage()
  .ref("Tenant Resumes/" + firebase.auth().currentUser.uid)
  .listAll()
  .then((res) => {
    res.items.forEach((result) => {
      result.getDownloadURL().then((docURL) => {
        setDocumentData((newURLs) => [...newURLs, docURL]);
        console.log(docURL);
      });
    });
  });
  };

  const sendMailFunction = async () => {
console.log(documentData);
const sendMailOverHTTP = firebase
  .functions()
  .httpsCallable("sendMailOverHTTP");
sendMailOverHTTP({
  replyTo: userInfo.email,
  name: userInfo.firstName + " " + userInfo.lastName,
  documentOne: documentData[0] ? documentData[0] : "",
  documentTwo: documentData[1] ? documentData[1] : "",
  documentThree: documentData[2] ? documentData[2] : "",
  documentFour: documentData[3] ? documentData[3] : "",
  documentFive: documentData[4] ? documentData[4] : "",
  documentSix: documentData[5] ? documentData[5] : "",
})
  .then((result) => {
    console.log(result);
  })
  .catch((error) => {
    console.log(error);
  });

}; };

How would I use this same methodology with a cloud function?我将如何对云 function 使用相同的方法?

I'm trying:我想:

    exports.sendAutomatedResume = functions.storage.bucket("Resumes/{uID}/Resume.pdf")
  .object()
  .onFinalize(async (object) => {

But it doesn't seem to be working.但它似乎没有用。 Any thoughts?有什么想法吗?

Cloud Storage triggers aren't able to pre-filter based on the path of the uploaded file, but you can create a generic onFinalize function that returns early if the path doesn't match. Cloud Storage 触发器无法根据上传文件的路径进行预过滤,但您可以创建一个通用的onFinalize function,如果路径不匹配,它会提前返回。 For example:例如:

exports.sendAutomatedResume = functions.storage.bucket().object()
  .onFinalize(async (object) => {
    const [folder, uid, filename, ...rest] = object.name.split("/");
    if (folder !== "Resumes" || filename !== "Resume.pdf" || rest.length > 0) {
      // doesn't match, so return early
      return;
    }

    // does match, do stuff here
  });

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

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