I have a Python code cloud function which gets triggered as soon as particular csv file comes in to a bucket. It reads that csv file & transforms the records in to JSON format & then publish it to the topic. below is the code:-
from google.cloud import storage
client = storage.Client()
import csv
import json
import datetime
import time
def test_multi_topic(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"Processing file: {file['name']}.")
print(f"file :{file}.")
bucket_name = 'ingka-cbdexpose-iipb'
bucket = client.get_bucket(bucket_name)
blob = bucket.get_blob('cbd/sync/cbd-data/test_file.csv')
dest_file = '/tmp/test_file.csv'
blob.download_to_filename(dest_file)
processedfilename = 'cbd/sync/processed/test_file' + str(datetime.datetime.now()) + '.csv'
blobprocessed = bucket.blob(processedfilename)
import pandas as pd
df = pd.read_csv(dest_file, encoding='utf-8', dtype=str)
df = df.assign(FILE_TYPE='TESTFL')
data = df.to_json(orient="records", lines=True).split('\n')
# jsonValue = data
from google.cloud import pubsub_v1
publisher = pubsub_v1.PublisherClient()
topic_1 = publisher.topic_path('ingka-cbd-exposecbd-devp', 'test-dev')
topic_2 = publisher.topic_path('ingka-cbd-exposecbd-devp', 'test2-dev')
for data in data:
data = data.encode("utf-8")
response_1 = publisher.publish(topic_1, data=data)
response_2 = publisher.publish(topic_2, data=data)
blob.delete()
try:
with open(dest_file, "rb") as my_file:
blobprocessed.upload_from_file(my_file)
except:
print("An exception occurred")
Now this code runs successful & my files does get published to the prescribed topic. But, it always logs an error that -"'NoneType' object has no attribute 'download_to_filename'". I found one stackflow answer on this link {https://stackoverflow.com/questions/51439904/attributeerror-nonetype-object-has-no-attribute-upload-from-filename}. when I implement this, I start getting different error as :- google.resumable_media.common.InvalidResponse: ('Request failed with status code', 404, 'Expected one of', <HTTPStatus.OK: 200>, <HTTPStatus.PARTIAL_CONTENT: 206>). Can anyone please help me in resolving the issue....