upload_file_s3.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import json
  2. import boto3
  3. #import mimetypes
  4. #import magic
  5. import uuid
  6. import config as cf
  7. from services.s3Service import S3Service
  8. from services.apiService import ApiService
  9. def upload_file_S3_handler(event, context):
  10. """ This method revolves around uploading a file to S3.
  11. Parameters
  12. event: Event generated from lambda
  13. """
  14. s3 = boto3.client("s3")
  15. file_name = event['headers']['file-name']
  16. user_id = event['headers']['user-id']
  17. file_content = event['body']
  18. bucket_name = cf.constants['bucket_name']
  19. file_id = str(uuid.uuid4())
  20. s3ServiceObj=S3Service()
  21. # Method call
  22. extension = s3ServiceObj.vald_file_checker(file_name)
  23. #print("The recieved value is",extension)
  24. if(extension == "invalid"):
  25. return {
  26. "statusCode": 400,
  27. "body": json.dumps(cf.response_message['fileFormat'])
  28. }
  29. try:
  30. response_upload_object = s3ServiceObj.upload_file_tos3(bucket_name,file_id,file_content)
  31. print(response_upload_object)
  32. if(response_upload_object['ResponseMetadata']['HTTPStatusCode']==200):
  33. response_upload_api_call_exec=upload_api_call_exec(file_id,file_name,user_id,extension)
  34. print("The response_upload-api_call_exec ",response_upload_api_call_exec)
  35. if(response_upload_api_call_exec!="failure"):
  36. return {
  37. "statusCode": 200,
  38. "body": response_upload_api_call_exec.content
  39. }
  40. else:
  41. return{
  42. "statusCode":200,
  43. "body":cf.response_message['file_upload_api_success']
  44. }
  45. else:
  46. return{
  47. "statusCode": 400,
  48. "body":cf.response_message['uploadException']
  49. }
  50. except:
  51. print("Issues while uploading file to s3")
  52. return {
  53. "statusCode": 400,
  54. "body": json.dumps(cf.response_message["uploadException"])
  55. }
  56. def upload_api_call_exec(file_id,file_name,user_id,extension):
  57. """ This function calls the Rest API to upload the file_id and other attributes to DynamoDB
  58. Parameters
  59. file_id : string
  60. The file_id which need to be created in DynamoDB.
  61. file_name : string
  62. The file_name corresponding to the file_id
  63. user_id : string
  64. The user_id corresponding to the file_id
  65. extension : string
  66. The extension of the file
  67. """
  68. try:
  69. apiService_obj = ApiService()
  70. content_api_call = apiService_obj.content_to_api_call(file_id, file_name, user_id, extension)
  71. print("The content api-call is",content_api_call)
  72. response_post_api_call = apiService_obj.post_api_call(content_api_call,cf.constants['post_back_url'])
  73. print("Reached here 4")
  74. print("The response_post_api call is",response_post_api_call)
  75. return response_post_api_call
  76. except:
  77. print("Failure in calling API")
  78. return "failure"
  79. #Question 1:
  80. # Whether status code need to be placed in config,json and use it from there?