MICListCC_1.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. def lambda_handler(event, context):
  2. try:
  3. excelURL = 'https://www.iso20022.org/sites/default/files/ISO10383_MIC/ISO10383_MIC.xls'
  4. imageRequest = requests.get(excelURL) # create HTTP response object
  5. fileName = 'ISO10383_MIC_Test.xls' # File Name to be saved as
  6. with open('/tmp/' + fileName, 'wb') as f:
  7. f.write(imageRequest.content)
  8. # Read 'MICs List by CC' sheet using Pandas library and create Dataframe
  9. # Store in /tmp folder
  10. fileLoad = pd.ExcelFile('/tmp/' + fileName)
  11. dfMIClistCC = fileLoad.parse('MICs List by CC')
  12. # Replace Nan from Dataframe and convert to empty string
  13. dfMIClistCC = dfMIClistCC.replace(np.nan, '')
  14. # Convert Panda Dataframe to Dictionary
  15. dfMIClistCC_dict = dfMIClistCC.to_dict('records')
  16. # Convert Dictionary to JSON
  17. dfMIClistCC_str = json.dumps(dfMIClistCC_dict)
  18. dfMIClistCC_json = json.loads(dfMIClistCC_str)
  19. # Save JSON file
  20. # Store in /tmp folder
  21. with open('/tmp/dfMIClistCC.json', 'w') as f:
  22. json.dump(dfMIClistCC_json, f)
  23. # Upload JSON file to S3 bucket
  24. data = open('/tmp/dfMIClistCC.json', 'rb')
  25. s3 = boto3.client('s3')
  26. s3.put_object(Bucket='bucket-miclistcc',
  27. Key='MICListCC.json',
  28. Body=data)
  29. return 'File uploaded successfully'
  30. except ConnectionError:
  31. print('Content does not exist')
  32. except FileNotFoundError:
  33. print('File not found')
  34. except Exception as e:
  35. print('Error occured: ' + str(e))