cloud function read storage object data without using the storage client

Viewed 23

I have created a simple cloud function with trigger: google.cloud.storage.object.v1.finalized

When a file (.xlsx) is being uploaded to my bucket I want to read it's content.

I am using following method for the same:

import functions_framework


@functions_framework.cloud_event
def process_data(cloud_event):
  print(f"Data: {cloud_event.data}")

I am able to print the cloud_event.data but how to I get the actual file which was uploaded

One way that I can do is using the storage client in below manner:

from google.cloud import storage
import functions_framework


def get_file(object_name, bucket_name, download_path):
  storage_client = storage.Client()
  bucket = storage_client.bucket(bucket_name)
  blob = bucket.blob(object_name)
  blob.download_to_filename(download_path)


@functions_framework.cloud_event
def process_data(cloud_event):
  print(f"Data: {cloud_event.data}")
  object_name = cloud_event.data['name']
  bucket_name = cloud_event.data['bucket']
  download_path = "/tmp/"
  get_file(object_name, bucket_name, download_path)

But is there a way through which I can get the actual contents of the file without using the cloud storage client ?

0 Answers
Related