Andrew Kehl
Andrew Kehl

Reputation: 17

Google cloud function python cannot interact with blobs

I'm trying to upload a file to a bucket, parse into a json and insert it into a table in bigquery. I am getting this error

AttributeError: 'str' object has no attribute 'blob'

I believe it is a depndency issue but I don't know how to solve it.

I am coding directly into the cloud function gui.

def create_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.
    """
    file = event
    print(f"********The file {file['name']} was created in {file['bucket']} at {event['timeCreated']}")
    
    
    bucket = file['bucket']
    blob = bucket.get_blob(file['name'])
    data = blob.download_as_string()
    table_id="tableid"
    client=bigquery.Client()
    client.insert_rows_json(table_id,[data])
    print(blob)

Upvotes: 0

Views: 1196

Answers (1)

marian.vladoi
marian.vladoi

Reputation: 8066

I think you should first create a storage Client object and the call the get_blob function.

Blobs / Objects

from google.cloud import storage
client = storage.Client()
bucket = client.get_bucket(file['bucket'])
blob = bucket.get_blob(file['name'])

Upvotes: 1

Related Questions