Search code examples
pythongoogle-cloud-platformgoogle-cloud-functionsgoogle-cloud-storageetl

How to write a Cloud Function to listen and respond events such as when a file is created, changed, or removed?


In GCP, I am having trouble finding an example on how to listen and respond to Cloud Storage events such as when a file is created, changed, or removed.

I tried looking at the GCP docs on how to do this, but there is nothing there. I am looking for a simple Python example using Cloud Functions to listen and respond to when a file is created, changed, or removed in my GCS Bucket.


Solution

  • You can trigger a Cloud Function V2 on a Cloud Storage event :

    gcloud functions deploy YOUR_FUNCTION_NAME \
    --gen2 \
    --trigger-event-filters="type=EVENT_TYPE" \
    --trigger-event-filters="bucket=YOUR_STORAGE_BUCKET" \
    ...
    
    • YOUR_FUNCTION_NAME is the Cloud Function name
    • EVENT_TYPE is the GCS file event (Object finalized, Object deleted
      ...). Check the link above on the doc for the different event types
    • YOUR_STORAGE_BUCKET is the GCS bucket concerned by the event

    For the code of the Cloud Function, you check this link :

    import functions_framework
    
    # Register a CloudEvent function with the Functions Framework
    @functions_framework.cloud_event
    def my_cloudevent_function(cloud_event):
      # Your code here
      # Access the CloudEvent data payload via cloud_event.data