2
votes

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

Inside my firebase storage a have a folder 'loads' and inside I have folders named with load id like:

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

https://ibb.co/NnQkTyC here's a screenshot of storage

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. In my app (TMS written with vuejs) on frontend I create confirmationOrder.pdf using html2canvas/jsPDF and then save it to storage/loads/${loadID}. And later on user can manually upload POD.pdf on the same bucket. When it happens I want my cloud function to merge these two pdfs in one new file (in same storage bucket). But again I don't know the bucket in advance.

Here's how I upload PDFs in frontend:

 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.

1
Can you show a picture of your Cloud Storage console. And if you have some existing, code , can you also share it?Are you sure you are using several buckets. Aren't you referring to different References?Renaud Tarnec
Please edit the question to show all relevant code so we can see what you're doing, including both client and server code that you have so far. You should explain exactly what is not working the way you expect with that code.Doug Stevenson

1 Answers

3
votes

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.

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. 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?".

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:

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.

You can then use some String methods to, for example, extract from this path the ${loadID} you refer to in your question.