blob-upload-4.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import os, uuid
  2. from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, __version__
  3. import argparse
  4. from argparse import ArgumentParser
  5. parser = ArgumentParser()
  6. parser.add_argument('--connect_str', default='', type=str)
  7. parser.add_argument('--container_name', default='', type=str)
  8. parser.add_argument('--source', default='', type=str)
  9. parser.add_argument('--target', default='', type=str)
  10. parser.add_argument('--is_directory', default=False, action='store_true')
  11. parser.add_argument('--download', default=False, action='store_true')
  12. parser.add_argument('--upload', default=False, action='store_true')
  13. arg = parser.parse_args()
  14. connect_str = arg.connect_str #Enter your connection string here! Refer to https://docs.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-python?tabs=environment-variable-windows for more info
  15. container_name = arg.container_name #Enter your continaer name from azure blob storage here!
  16. blob_service_client = BlobServiceClient.from_connection_string(connect_str) # Create the BlobServiceClient object which will be used to create a container client
  17. def upload_file_to_blob(upload_file_path, target): #file path - >file path
  18. blob_client = blob_service_client.get_blob_client(container=container_name, blob=target)
  19. print("\nUploading to Azure Storage as blob:\n\t" + upload_file_path)
  20. with open(upload_file_path, "rb") as data:
  21. blob_client.upload_blob(data)
  22. def upload_directory_to_blob(upload_file_path, target): #directory name -> directory name
  23. print("\nUploading directory to Azure Storage as blob:\n\t" + upload_file_path)
  24. files = os.listdir(upload_file_path)
  25. for dir in files:
  26. file_name = upload_file_path + '/' + dir
  27. target_ = target+ '/' + dir
  28. blob_client = blob_service_client.get_blob_client(container=container_name, blob=target_)
  29. with open(file_name, "rb") as data:
  30. blob_client.upload_blob(data)
  31. def download_file_from_blob(source, download_file_path):
  32. blob_client = blob_service_client.get_blob_client(container=container_name, blob=source)
  33. print("\nDownloading blob to \n\t from container" + download_file_path)
  34. with open(download_file_path, "wb") as download_file:
  35. download_file.write(blob_client.download_blob().readall())
  36. def download_directory_from_blob(source, download_directory_path):
  37. container_client = ContainerClient.from_connection_string(conn_str=connect_str, container_name=container_name)
  38. print(f"\nDownloading all blobs from the following directory {source} in container {container_name}")
  39. blob_list = container_client.list_blobs()
  40. for blob in blob_list:
  41. if source in blob.name:
  42. blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob.name)
  43. os.makedirs(os.path.dirname(blob.name), exist_ok=True)
  44. with open(blob.name, "wb") as download_file:
  45. download_file.write(blob_client.download_blob().readall())
  46. if not arg.download and not arg.upload:
  47. raise Exception('Specificy either --upload or --download. Specify only one.')
  48. if arg.download: #downloading from source to target
  49. if not arg.is_directory:
  50. download_file_from_blob(arg.source, arg.target)
  51. else:
  52. download_directory_from_blob(arg.source, arg.target)
  53. else: #Uploading source to target
  54. if not arg.is_directory:
  55. upload_file_to_blob(arg.source, arg.target)
  56. else:
  57. upload_directory_to_blob(arg.source, arg.target)