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.
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
nameEVENT_TYPE
is the GCS
file event (Object finalized, Object deletedYOUR_STORAGE_BUCKET
is the GCS
bucket concerned by the eventFor 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