uploader.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import json
  2. from amazon_cloud_service import Amazon_cloud_operations
  3. from google_cloud_service import Goggle_cloud_operations
  4. from local_files_collector import Local_Files
  5. from config import amazon_credential_list, types_of_files
  6. class Uploader(object):
  7. """Uploader is a wrapper class which combines all in one place"""
  8. def __init__(self, file_path=None, amazon_credentail_json=None,
  9. google_client_credentail_json=None,
  10. google_client_bucket=None,
  11. google_client_region=None,
  12. google_client_config_path=None,
  13. new_file_dict=None):
  14. self.list_of_self = None
  15. self.file_path = Local_Files(file_path, new_file_dict)
  16. self.__amazon_credentail_json = amazon_credentail_json
  17. self.__google_client_credentail_json = google_client_credentail_json
  18. self.__google_client_bucket = google_client_bucket
  19. self.__google_client_region = google_client_region
  20. self.__google_client_config_path = google_client_config_path
  21. self.list_input_file = self.file_path.get_files_dict()
  22. print(" Uploader object created")
  23. def read_json(self, jsonfile):
  24. try:
  25. with open(jsonfile, 'r') as json_file:
  26. self.key_dict = json.loads(json_file.read())
  27. return self.key_dict
  28. except:
  29. raise Exception('Given Configuration json file not found')
  30. def upload(self, service_type="Auto", category_type="All"):
  31. """Upload is main method
  32. Service_type can be Amazon,Google,Auto
  33. Category_type can be 'Documents','Media_files','Images','Images and Media files'
  34. Note: If service type is Auto or default empty then Category_type will not play a role.
  35. Image and multi media files will be transfred to Amazon s3 and Documents will be sent to google storage.
  36. """
  37. print(self.list_input_file)
  38. if service_type == "Amazon":
  39. # if service type is Amazon then all the operations are done here
  40. amazon_credential_file = self.read_json(
  41. self.__amazon_credentail_json)
  42. amazon = Amazon_cloud_operations(
  43. aws_access_key=amazon_credential_file[
  44. amazon_credential_list[0]],
  45. aws_secret_access_key=amazon_credential_file[
  46. amazon_credential_list[1]],
  47. s3_bucket=amazon_credential_file[amazon_credential_list[2]],
  48. region=amazon_credential_file[amazon_credential_list[3]])
  49. if category_type == 'Documents':
  50. if types_of_files[0] in self.list_input_file.keys():
  51. amazon.upload_files(self.list_input_file['documents'])
  52. elif category_type == 'Media files':
  53. if types_of_files[1] in self.list_input_file.keys():
  54. amazon.upload_files(self.list_input_file['media_files'])
  55. elif category_type == 'Images':
  56. if types_of_files[2] in self.list_input_file.keys():
  57. amazon.upload_files(self.list_input_file['images'])
  58. elif category_type == 'Images and Media files':
  59. if types_of_files[1] in self.list_input_file.keys():
  60. amazon.upload_files(self.list_input_file['media_files'])
  61. if types_of_files[2] in self.list_input_file.keys():
  62. amazon.upload_files(self.list_input_file['images'])
  63. else:
  64. if types_of_files[1] in self.list_input_file.keys():
  65. amazon.upload_files(self.list_input_file['media_files'])
  66. if types_of_files[2] in self.list_input_file.keys():
  67. amazon.upload_files(self.list_input_file['images'])
  68. if types_of_files[0] in self.list_input_file.keys():
  69. amazon.upload_files(self.list_input_file['documents'])
  70. elif service_type == "Google":
  71. # if service type is Google then all the operations are done here
  72. google = Goggle_cloud_operations(
  73. bucket=self.__google_client_bucket,
  74. region=self.__google_client_region,
  75. client_config_path=self.__google_client_config_path)
  76. if category_type == 'Documents':
  77. if types_of_files[0] in self.list_input_file.keys():
  78. google.upload_files(self.list_input_file['documents'])
  79. elif category_type == 'Media files':
  80. if types_of_files[1] in self.list_input_file.keys():
  81. google.upload_files(self.list_input_file['media_files'])
  82. elif category_type == 'Images':
  83. if types_of_files[2] in self.list_input_file.keys():
  84. google.upload_files(self.list_input_file['images'])
  85. elif category_type == 'Images and Media files':
  86. if types_of_files[1] in self.list_input_file.keys():
  87. google.upload_files(self.list_input_file['media_files'])
  88. if types_of_files[2] in self.list_input_file.keys():
  89. google.upload_files(self.list_input_file['images'])
  90. else:
  91. if types_of_files[1] in self.list_input_file.keys():
  92. google.upload_files(self.list_input_file['media_files'])
  93. if types_of_files[2] in self.list_input_file.keys():
  94. google.upload_files(self.list_input_file['images'])
  95. if types_of_files[0] in self.list_input_file.keys():
  96. google.upload_files(self.list_input_file['documents'])
  97. else:
  98. # if its default or Auto then Image and multi media files will be transfred to Amazon s3 and Documents will be sent to google storage.
  99. google = Goggle_cloud_operations(
  100. bucket=self.__google_client_bucket,
  101. region=self.__google_client_region,
  102. client_config_path=self.__google_client_config_path)
  103. amazon_credential_file = self.read_json(
  104. self.__amazon_credentail_json)
  105. amazon = Amazon_cloud_operations(
  106. aws_access_key=amazon_credential_file[
  107. amazon_credential_list[0]],
  108. aws_secret_access_key=amazon_credential_file[
  109. amazon_credential_list[1]],
  110. s3_bucket=amazon_credential_file[amazon_credential_list[2]],
  111. region=amazon_credential_file[amazon_credential_list[3]])
  112. if types_of_files[1] in self.list_input_file.keys():
  113. amazon.upload_files(self.list_input_file['media_files'])
  114. if types_of_files[2] in self.list_input_file.keys():
  115. amazon.upload_files(self.list_input_file['images'])
  116. if types_of_files[0] in self.list_input_file.keys():
  117. google.upload_files(self.list_input_file['documents'])