upload_file_s3_1.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. def upload_file_S3_handler(event, context):
  2. """ This method revolves around uploading a file to S3.
  3. Parameters
  4. event: Event generated from lambda
  5. """
  6. s3 = boto3.client("s3")
  7. file_name = event['headers']['file-name']
  8. user_id = event['headers']['user-id']
  9. file_content = event['body']
  10. bucket_name = cf.constants['bucket_name']
  11. file_id = str(uuid.uuid4())
  12. s3ServiceObj=S3Service()
  13. # Method call
  14. extension = s3ServiceObj.vald_file_checker(file_name)
  15. #print("The recieved value is",extension)
  16. if(extension == "invalid"):
  17. return {
  18. "statusCode": 400,
  19. "body": json.dumps(cf.response_message['fileFormat'])
  20. }
  21. try:
  22. response_upload_object = s3ServiceObj.upload_file_tos3(bucket_name,file_id,file_content)
  23. print(response_upload_object)
  24. if(response_upload_object['ResponseMetadata']['HTTPStatusCode']==200):
  25. response_upload_api_call_exec=upload_api_call_exec(file_id,file_name,user_id,extension)
  26. print("The response_upload-api_call_exec ",response_upload_api_call_exec)
  27. if(response_upload_api_call_exec!="failure"):
  28. return {
  29. "statusCode": 200,
  30. "body": response_upload_api_call_exec.content
  31. }
  32. else:
  33. return{
  34. "statusCode":200,
  35. "body":cf.response_message['file_upload_api_success']
  36. }
  37. else:
  38. return{
  39. "statusCode": 400,
  40. "body":cf.response_message['uploadException']
  41. }
  42. except:
  43. print("Issues while uploading file to s3")
  44. return {
  45. "statusCode": 400,
  46. "body": json.dumps(cf.response_message["uploadException"])
  47. }