I have a cloud function that is meant to create a CSV from an API call and then send that CSV to Cloud Storage.
Here is my code:
import requests
import pprint
import pandas as pd
from flatsplode import flatsplode
import csv
import datetime
import schedule
import time
import json
import numpy as np
import os
import tempfile
from google.cloud import storage
api_url = 'https://[YOUR_DOMAIN].com/api/v2/[API_KEY]/keywords/list?site_id=[SITE_ID][&start={start}][&results=100]&format=json'
def export_data(url):
response = requests.get(url) # Make a GET request to the URL
payload = response.json() # Parse `response.text` into JSON
pp = pprint.PrettyPrinter(indent=1)
# Use the flatsplode package to quickly turn the JSON response to a DF
new_list = pd.DataFrame(list(flatsplode(payload)))
# Drop certain columns from the DF
idx = np.r_[1:5,14:27,34,35]
new_list = new_list.drop(new_list.columns[idx], axis=1)
# Create a csv and load it to google cloud storage
new_list = new_list.to_csv('/tmp/temp.csv')
def upload_blob(bucket_name, source_file_name, destination_blob_name):
storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)
blob = bucket.blob(destination_blob_name)
blob.upload_from_file(source_file_name)
message = "Data for CSV file" # ERROR HERE
csv = open(new_list, "w")
csv.write(message)
with open(new_list, 'r') as file_obj:
upload_blob('data-exports', file_obj, 'data-' + str(datetime.date.today()) + '.csv')
export_data(api_url)
I attempted to have the file in the /tmp format to allow me to write it to storage but haven't had much success. The API call works like a charm and I am able to get a CSV locally. The upload to Cloud Storage is where I get the error.
Any help is much appreciated!