Skip to content Skip to sidebar Skip to footer

How To Access Csv File From Google Cloud Storage In A Google Cloud Function Via Pandas?

I'm new to cloud functions, so I followed the default GCP cloud function 'hello world' tutorial. It worked fine and printed 'hello world' as expected. I only changed the requiremen

Solution 1:

You are new on Cloud Functions and there are some stuff to know and some trap to avoid. One of them: Cloud Functions is stateless, you can't write on the file system.

Except on the /tmp directory. It's a in memory file system (size correctly your Cloud Functions memory size to take into account your app memory footprint + the file size stored in the /tmp dir)

Update your Cloud Function like that

....
    else:       
        storage_client = storage.Client()
        bucket = storage_client.bucket('my_bucket')
        model_filename = "my_file.csv"
        blob = bucket.blob(model_filename)
        blob.download_to_filename('/tmp/temp.csv')        
        with open('/tmp/temp.csv','rb') as f:
            df = pd.read_csv(f)
        
        return str(df.columns)


Post a Comment for "How To Access Csv File From Google Cloud Storage In A Google Cloud Function Via Pandas?"