nszmmp
nszmmp

Reputation: 109

Get return from onFinalize Firebase cloud function

I've created an unzip onFinalize function, which unzip any file which copied to storage, then delete the file, which is working well, but I want to get return from the onFinalize cloud function to my Angular app, or listening somehow to know when finished the process.

Now I use logging, but its annoying, because the rest call to logger api use the accessToken, and its impossible to refresh the accessToken after expired in 3600s (only if the user logout, and login again its counting again)

Anyway is there a good solution in Angular without rest call, or without access token?

export const manageZipArchives = functions
  .region("xxxxx")
  .runWith({timeoutSeconds: 540, memory: "512MB"})
  .storage.bucket("xxxxxxxxx")
  .object()
  .onFinalize(async (obj: functions.storage.ObjectMetadata) => {
    const file = admin
      .storage()
      .bucket(obj.bucket)
      .file(obj.name!);

    ...

    return ok;
  });

Upvotes: 0

Views: 261

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83103

It's very strange, that I can't listening, or get return from function (from your comments)

Cloud Functions triggered by Cloud Storage events are background Cloud Functions. They don't have any notion of a client (or a frontend): the file created in Cloud Storage could have been created by a backend process, like an app running on a Compute Engine instance for example.

In your case, you can get advantage of the realtime listeners offered by either Firestore or the Realtime Database.

Let's take the example of a Firestore document. The process would be as follows:

In your Angular app:

  1. Generate a document Reference with the doc() method, in a specific collection.
  2. Get the doc ID corresponding to this Reference, with the id property.
  3. Assign this doc ID as a Custom Metadata of the file you upload.
  4. Set a listener to this document Reference with the onSnapshot() method. The doc does not exist yet, but this is not a problem: when it will be created the listener will be fired.
  5. Upload the file

In your Cloud Function:

  1. When all the asynchronous work of unziping, deletion, etc... is complete, get the value of the document ID stored as a Custom Metadata value and create the document in Firestore => The listener in the angular app will be triggered and you know that the work for this file is complete.

Note that if you want to report a potential error or exception to the frontend you could also use the Firestore doc: for example you can add a status field and an errorMessage field with the desired info. In the listener you'll get the value of those fields.

Upvotes: 1

Related Questions