blob-adapter.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import configparser
  2. from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, __version__
  3. from azure.core.exceptions import HttpResponseError, ResourceExistsError
  4. from flask import jsonify
  5. class AzureBlobAdapter:
  6. FILE_PREFIX = 'IN_CARE'
  7. blob_service_client: BlobServiceClient
  8. blob_client: BlobClient
  9. container_client: ContainerClient
  10. configs = configparser.ConfigParser()
  11. configs.read('azure_blob.cfg')
  12. # init method or constructor
  13. def __init__(self):
  14. connection_string = self.get_config('connection_string')
  15. print("Azure Blob Storage v" + __version__ +
  16. " - Blob Python libs")
  17. self.blob_service_client = BlobServiceClient.from_connection_string(
  18. connection_string)
  19. def upload(self, file_dict):
  20. upload_response = {}
  21. for key in file_dict:
  22. print("File Dict Key: [{}] value is: {}".format(key, file_dict[key]))
  23. print("\nUploading to Azure Storage as blob:\n\t" + key)
  24. self.blob_client = self.blob_service_client.get_blob_client(container=self.get_config('container_name'), blob=key)
  25. with open(file_dict[key], "rb") as data:
  26. try:
  27. self.blob_client.upload_blob(data)
  28. print('File: Uploaded Successfully: {}'.format(key))
  29. upload_response[key] = 'Successfully Uploaded'
  30. except ResourceExistsError:
  31. print('File: NOT Uploaded Successfully: {}'.format(key))
  32. upload_response[key] = 'This Resource already exists'
  33. upload_response['Partial'] = True
  34. print('This Resource already exists')
  35. # return 'This Resource already exists'
  36. print("Before Returning Response:")
  37. print(jsonify(upload_response))
  38. print("---------------")
  39. return upload_response
  40. def get_blob_client(self, blob_name):
  41. self.blob_client = self.blob_service_client.get_blob_client(
  42. container=self.get_config('container_name'), blob=blob_name)
  43. return self.blob_client
  44. def list_blobs(self):
  45. print("\nList blobs in the container")
  46. self.container_client = self.blob_service_client.get_container_client(
  47. container=self.get_config('container_name'))
  48. blob_list = self.container_client.list_blobs()
  49. blobs = []
  50. for blob in blob_list:
  51. # print("\t Blob name: " + blob.name)
  52. blobs.append(blob.name)
  53. return blobs
  54. def get_config(self, app_property):
  55. config_value = self.configs['azure_blob_config'][app_property]
  56. return config_value