When an file is successfully uploaded to a given Google Cloud Storage bucket ("Object Finalize"), I would like to set up a trigger to let this filename be accessible within a VM which is running.
There is a standard Cloud Function which listens to when the file has been uploaded, with the trigger google.storage.object.finalize
:
def hello_gcs(event, context):
"""Background Cloud Function to be triggered by Cloud Storage.
This generic function logs relevant data when a file is changed.
Args:
event (dict): The dictionary with data specific to this type of event.
The `data` field contains a description of the event in
the Cloud Storage `object` format described here:
https://cloud.google.com/storage/docs/json_api/v1/objects#resource
context (google.cloud.functions.Context): Metadata of triggering event.
Returns:
None; the output is written to Stackdriver Logging
"""
print('Event ID: {}'.format(context.event_id))
print('Event type: {}'.format(context.event_type))
print('Bucket: {}'.format(event['bucket']))
print('File: {}'.format(event['name']))
print('Metageneration: {}'.format(event['metageneration']))
print('Created: {}'.format(event['timeCreated']))
print('Updated: {}'.format(event['updated']))
https://cloud.google.com/functions/docs/calling/storage#functions-calling-storage-python
(I'm using Python but I'm happy to use any of the other languages provided)
Let's say I have a VM named 'my-instance': Is there a way to pass the filename from event['name']
to the VM such that the code in the VM can access this?
There are other SO questions which discuss how to read files directly from Cloud Storage, e.g. Read csv from Google Cloud storage to pandas dataframe
import pandas as pd
import gcsfs
fs = gcsfs.GCSFileSystem(project='my-project')
with fs.open('bucket/path.csv') as f:
df = pd.read_csv(f)
But how can I pass the filename from the google.storage.object.finalize
to the VM for this code to run?