blob-upload-1.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import logging
  2. import os
  3. from azure.storage.blob import BlobServiceClient
  4. from azurebatchload.core import Base
  5. class Upload(Base):
  6. def __init__(
  7. self,
  8. destination,
  9. source,
  10. folder=None,
  11. extension=None,
  12. method="batch",
  13. modified_since=None,
  14. overwrite=False,
  15. list_files=None,
  16. create_download_links=False,
  17. expiry_download_links=7,
  18. ):
  19. super(Upload, self).__init__(
  20. destination=destination,
  21. folder=source,
  22. extension=extension,
  23. modified_since=modified_since,
  24. method=method,
  25. list_files=list_files,
  26. expiry_download_links=expiry_download_links,
  27. )
  28. self.blob_folder = folder
  29. self.overwrite = overwrite
  30. self.create_download_links = create_download_links
  31. def upload_batch(self):
  32. cmd = f"az storage fs directory upload " f"-f {self.destination} " f"-s {self.folder} -r"
  33. non_default = {"-d": self.blob_folder, "--connection-string": self.connection_string}
  34. for flag, value in non_default.items():
  35. if value:
  36. cmd = f"{cmd} {flag} '{value}'"
  37. os.system(cmd)
  38. def upload_single(self):
  39. blob_service_client = BlobServiceClient.from_connection_string(self.connection_string)
  40. download_links = {}
  41. for root, dirs, files in os.walk(self.folder):
  42. for file in files:
  43. full_path = os.path.join(root, file)
  44. # ignore hidden files
  45. if file.startswith("."):
  46. continue
  47. # if list_files is given, only upload matched files
  48. if self.list_files and file not in self.list_files:
  49. continue
  50. # if extension is given only upload if extension is matched
  51. if self.extension and os.path.isfile(full_path) and not file.lower().endswith(self.extension.lower()):
  52. continue
  53. blob_folder = root.replace(self.folder, "").lstrip("/")
  54. if self.blob_folder:
  55. # we only want to append blob_folder if it actually is a path or folder
  56. # blob_folder can be empty string ""
  57. if blob_folder:
  58. blob_folder = os.path.join(self.blob_folder, blob_folder)
  59. else:
  60. blob_folder = self.blob_folder
  61. # if no folder is given, just upload to the container root path
  62. if not blob_folder:
  63. container = self.destination
  64. else:
  65. container = os.path.join(self.destination, blob_folder)
  66. container_client = blob_service_client.get_container_client(container=container)
  67. with open(full_path, "rb") as data:
  68. logging.debug(f"Uploading blob {full_path}")
  69. container_client.upload_blob(data=data, name=file, overwrite=self.overwrite)
  70. if self.create_download_links:
  71. download_links[file] = self.create_blob_link(blob_folder=blob_folder, blob_name=file)
  72. return download_links
  73. def upload(self):
  74. self.checks()
  75. logging.info(f"Uploading to container {self.destination} with method = '{self.method}'.")
  76. if self.method == "batch":
  77. return self.upload_batch()
  78. else:
  79. return self.upload_single()