Search code examples
firebasegoogle-cloud-functionsfirebase-storageregion

Different Google Cloud Function Call For Different Storage?


I'm currently working on a project where there are two different storage buckets (one in US central and another in S. Korea).

It would be more efficient both cost-wise and speed-wise to locate the functions for updating storage usage in same location as storage.

But, now I want to manage the function at two different location. This function is meant to update the Firestore whenever new image is uploaded in a storage.

This is the code that I thought would work (but actually don't)

exports.incrementUsedSize = functions.region('us-central1').storage.object().onFinalize
exports.incrementUsedSizeKr = functions.region('asia-northeast3').storage.object().onFinalize

But both of these are called whenever storage at US or S. Korea is updated.

Is there a way to make these functions work at two locations without interfering each other?

I mean, is there a way to restrict the scope of the function to the storage at specific location?


Solution

  • As per the documentation:

    To set the region where a function runs, set the region parameter in the function definition as shown:

    exports.incrementUsedSize = functions
        .region('us-central1')
        .storage
        .object()
        .onFinalize((object) => {
          // ...
        });
    

    The .region() sets the location for your cloud function and has nothing to do with the bucket.

    However, as you say the buckets are different and not a single "dual-region" bucket, you should be able to deploy this way:

    gcloud functions deploy incrementUsedSize \
    --runtime nodejs10 \
    --trigger-resource YOUR_TRIGGER_BUCKET_NAME \
    --trigger-event google.storage.object.finalize
    

    by specifying the trigger bucket you should be able to restrict the invocation trigger. More details in the documentation here

    If your question refers to Firebase Cloud Function then you can specify the bucket name as mentioned here:

    exports.incrementUsedSize = functions.storage.bucket('bucketName').object().onFinalize(async (object) => {
      // ...
    });