import os, uuid
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, __version__

connect_str = "DefaultEndpointsProtocol=https;AccountName=continual;AccountKey=EdI7/lDMwxfLhTaCy/4tnfarcgExcrC5rrRi1vfVXxnX/ICuYntCVgEjuqq6lXk+pF04uUeLVqeqssMQ4uAJEg==;EndpointSuffix=core.windows.net"
container_name = 'roe'

# Create the BlobServiceClient object which will be used to create a container client
blob_service_client = BlobServiceClient.from_connection_string(connect_str)

def upload_file_to_blob(upload_file_path, target): #file path - >file path
    blob_client = blob_service_client.get_blob_client(container=container_name, blob=target)
    print("\nUploading to Azure Storage as blob:\n\t" + upload_file_path)
    with open(upload_file_path, "rb") as data:
        blob_client.upload_blob(data)

def upload_directory_to_blob(upload_file_path, target): #directory name -> directory name
    print("\nUploading directory to Azure Storage as blob:\n\t" + upload_file_path)
    files = os.listdir(upload_file_path)
    for dir in files:
        file_name = upload_file_path + '/' + dir
        target_ = target+ '/' + dir
        blob_client = blob_service_client.get_blob_client(container=container_name, blob=target_)
        with open(file_name, "rb") as data:
            blob_client.upload_blob(data)

def download_file_from_blob(source, download_file_path):
    blob_client = blob_service_client.get_blob_client(container=container_name, blob=source)
    print("\nDownloading blob to \n\t from container" + download_file_path)

    with open(download_file_path, "wb") as download_file:
        download_file.write(blob_client.download_blob().readall())

def download_directory_from_blob(source, download_directory_path):
    container_client = ContainerClient.from_connection_string(conn_str=connect_str, container_name=container_name)
    print(f"\nDownloading all blobs from the following directory {source} in container {container_name}")
    blob_list = container_client.list_blobs()
    for blob in blob_list:
        if source in blob.name:
            print(f'processing :{blob.name}')
            blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob.name)
            os.makedirs(os.path.dirname(blob.name), exist_ok=True)
            with open(blob.name, "wb") as download_file:
                download_file.write(blob_client.download_blob().readall())

download_directory_from_blob('kilt_wikipedia', 'data')