The need is to read the json file data present in GCS bucket using cloud functions (python). I have uploaded the required json data to bucket and try running the cloud function.
The input json file data :
{"count":15,"data":[{"P_ID":21.0,"P_NAME":"MMME","TZ":"PST","DATE_MODIFIED":"2005-10-14 00:00:00"}]}
here is the code:
from google.cloud import storage
import base64
import json
import os
def hello_gcs(event, context):
"""
Triggered by a change to a Cloud Storage bucket.
Args:
event (dict): Event payload.
context (google.cloud.functions.Context): Metadata for the event.
"""
print("in CF")
print("event", event)
file_name = event['name']
bucket_name = event['bucket']
client = storage.Client()
bucket = client.get_bucket(bucket_name)
file_blob = storage.Blob(file_name, bucket)
download_data = file_blob.download_as_string().decode()
jsondata = {}
jsondata = download_data
print("download_data : ", download_data)
print("jsondata := ", jsondata)
print(jsondata['count'])
Please correct me. I am not getting the data displayed in the code. this is just to test the cloud function once this is working I need to implement additional features.
The function that you use if triggered on an event, when an object is written to Cloud Storage for instance. But you only have the event, not the content. With the event data, you have all that you need for downloading the file and to process it.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments