importboto3importosimportos.pathimporturllib.parseimportpandasaspdimportrequestsimportjson# 10,000 maximum allowed payloadREPORT_ROWS=int(os.environ["REPORT_ROWS"])DR_API_TOKEN=os.environ["DR_API_TOKEN"]DR_INSTANCE=os.environ["DR_INSTANCE"]s3=boto3.resource("s3")defreport_rows(list_to_report,url,total):print("reporting "+str(len(list_to_report))+" records!")df=pd.DataFrame(list_to_report)# this must be provided as a stringdf["associationId"]=df["associationId"].apply(str)report_json=json.dumps({"data":df.to_dict("records")})response=requests.post(url,data=report_json,headers={"Authorization":"Bearer "+DR_API_TOKEN,"Content-Type":"application/json",},)print("response status code: "+str(response.status_code))ifresponse.status_code==202:print("success! reported "+str(total)+" total records!")else:print("error reporting!")print("response content: "+str(response.content))deflambda_handler(event,context):# get the object that triggered lambdabucket=event["Records"][0]["s3"]["bucket"]["name"]key=urllib.parse.unquote_plus(event["Records"][0]["s3"]["object"]["key"],encoding="utf-8")filenm=os.path.basename(key)fulldir=os.path.dirname(key)deployment=os.path.basename(fulldir)print("bucket is "+bucket)print("key is "+key)print("filenm is "+filenm)print("fulldir is "+fulldir)print("deployment is "+deployment)url=DR_INSTANCE+"/api/v2/deployments/"+deployment+"/actuals/fromJSON/"session=boto3.session.Session()client=session.client("s3")line_no=-1total=0rows_list=[]forlinesinclient.get_object(Bucket=bucket,Key=key)["Body"].iter_lines():# if the header, make sure the case sensitive required fields are presentifline_no==-1:header=lines.decode("utf-8").split(",")col1=header[0]col2=header[1]expectedHeaders=["associationId","actualValue"]ifcol1notinexpectedHeadersorcol2notinexpectedHeaders:print("ERROR: data must be csv with 2 columns, headers case sensitive: associationId and actualValue")breakelse:line_no=0else:input_dict={}input_row=lines.decode("utf-8").split(",")input_dict.update({col1:input_row[0]})input_dict.update({col2:input_row[1]})rows_list.append(input_dict)line_no+=1total+=1ifline_no==REPORT_ROWS:report_rows(rows_list,url,total)rows_list=[]line_no=0ifline_no>0:report_rows(rows_list,url,total)# delete the processed inputs3.Object(bucket,key).delete()