blob-permission.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. from datetime import datetime, timedelta
  2. from azure.storage.blob import BlobSasPermissions, generate_blob_sas
  3. from azurebatchload.checks import Checks
  4. class Base(Checks):
  5. def __init__(
  6. self,
  7. destination,
  8. folder,
  9. extension=None,
  10. modified_since=None,
  11. method="batch",
  12. list_files=None,
  13. expiry_download_links=7,
  14. ):
  15. super().__init__(directory=folder)
  16. self.destination = destination
  17. self.folder = folder
  18. self.extension = extension
  19. self.modified_since = modified_since
  20. if not self._check_azure_cli_installed():
  21. self.method = "single"
  22. else:
  23. self.method = method
  24. self.list_files = list_files
  25. credentials = self._check_connection_credentials()
  26. self.connection_string = credentials[0]
  27. self.account_name = credentials[1]
  28. self.account_key = credentials[2]
  29. self.expiry_download_links = expiry_download_links
  30. def checks(self):
  31. allowed_methods = ("batch", "single")
  32. if self.method not in allowed_methods:
  33. raise ValueError(f"Method {self.method} is not a valid method. Choose from {' or '.join(allowed_methods)}.")
  34. if self.list_files and self.method == "batch":
  35. raise ValueError("list_files is only allowed with method='single'.")
  36. if self.list_files and not isinstance(self.list_files, list):
  37. raise ValueError(f"Argument list_files was set, but is not of type list, but type {type(self.list_files)}")
  38. def create_blob_link(self, blob_folder, blob_name) -> str:
  39. if blob_folder:
  40. full_path_blob = f"{blob_folder}/{blob_name}"
  41. else:
  42. full_path_blob = blob_name
  43. url = f"https://{self.account_name}.blob.core.windows.net/{self.destination}/{full_path_blob}"
  44. sas_token = generate_blob_sas(
  45. account_name=self.account_name,
  46. account_key=self.account_key,
  47. container_name=self.destination,
  48. blob_name=full_path_blob,
  49. permission=BlobSasPermissions(read=True, delete_previous_version=False),
  50. expiry=datetime.utcnow() + timedelta(days=self.expiry_download_links),
  51. )
  52. url_with_sas = f"{url}?{sas_token}"
  53. return url_with_sas
  54. @staticmethod
  55. def create_not_case_sensitive_extension(extension):
  56. """
  57. We create in-case sensitive fnmatch
  58. .pdf -> .[Pp][Dd][Ff]
  59. .csv -> .[Cc][Ss][Vv]
  60. """
  61. new_extension = ""
  62. for letter in extension:
  63. if not letter.isalpha():
  64. new_extension += letter
  65. else:
  66. new_extension += f"[{letter.upper()}{letter}]"
  67. if not new_extension.startswith("*"):
  68. new_extension = "*" + new_extension
  69. return new_extension
  70. def define_pattern(self):
  71. self.extension = self.create_not_case_sensitive_extension(self.extension)
  72. if self.folder and not self.extension:
  73. if self.folder.endswith("/"):
  74. pattern = self.folder + "*"
  75. else:
  76. pattern = self.folder + "/*"
  77. elif self.folder and self.extension:
  78. pattern = self.folder.rstrip("/") + "/" + "*" + self.extension
  79. elif not self.folder and self.extension:
  80. pattern = "*" + self.extension
  81. else:
  82. pattern = None
  83. return pattern