blob-upload-1_3.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. def upload_single(self):
  2. blob_service_client = BlobServiceClient.from_connection_string(self.connection_string)
  3. download_links = {}
  4. for root, dirs, files in os.walk(self.folder):
  5. for file in files:
  6. full_path = os.path.join(root, file)
  7. # ignore hidden files
  8. if file.startswith("."):
  9. continue
  10. # if list_files is given, only upload matched files
  11. if self.list_files and file not in self.list_files:
  12. continue
  13. # if extension is given only upload if extension is matched
  14. if self.extension and os.path.isfile(full_path) and not file.lower().endswith(self.extension.lower()):
  15. continue
  16. blob_folder = root.replace(self.folder, "").lstrip("/")
  17. if self.blob_folder:
  18. # we only want to append blob_folder if it actually is a path or folder
  19. # blob_folder can be empty string ""
  20. if blob_folder:
  21. blob_folder = os.path.join(self.blob_folder, blob_folder)
  22. else:
  23. blob_folder = self.blob_folder
  24. # if no folder is given, just upload to the container root path
  25. if not blob_folder:
  26. container = self.destination
  27. else:
  28. container = os.path.join(self.destination, blob_folder)
  29. container_client = blob_service_client.get_container_client(container=container)
  30. with open(full_path, "rb") as data:
  31. logging.debug(f"Uploading blob {full_path}")
  32. container_client.upload_blob(data=data, name=file, overwrite=self.overwrite)
  33. if self.create_download_links:
  34. download_links[file] = self.create_blob_link(blob_folder=blob_folder, blob_name=file)
  35. return download_links