2

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.

0

2 Answers 2

4

here is the working code. Converted the download file to json object and accessed the fields.

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()
    print("download_data : ", download_data)
    """
    jsondata = {}
    jsondata = download_data
    """
    jsondata = {}
    #convert string to  object
    jsondata = json.loads(download_data)
    print("jsondata => ", jsondata)
    print(jsondata['count'])
Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

Hi, as you said the trigger is based on the file creation in the storage bucket. In my code I am downloading it but not able to see the data (on print). can you please suggest if my code is right or anything to be added.
Why do you decode your string? Did you store the content in b64? If not, remove the decode and print the data. It should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.