I have a code that reads a directory for files and upload the files to Azure Blob storage. It works well and file upload it successful. However, I need help to modify this code to run async operations to have concurrent upload.
import os
import asyncio
import yaml
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, __version__
def load_config():
path_root = os.path.dirname(os.path.abspath(__file__))
with open(path_root + "/config.yaml", "r") as configfile:
return yaml.load(configfile, Loader=yaml.FullLoader)
def read_files(dir):
with os.scandir(dir) as files:
for filename in files:
if filename.is_file() and not filename.name.startswith('.'):
yield filename
def upload(files, connection_string, container_name):
container_client = ContainerClient.from_connection_string(connection_string, container_name)
print("Uploading images to remote blob storage")
for file in files:
blob_client = container_client.get_blob_client(file.name)
with open(file.path, "rb") as data:
blob_client.upload_blob(data)
print(f"{file.name} upload to remote blob storage")
config = load_config()
images = read_files(config['source_folder'] + '/images')
upload(images, config['azure_storage_connectionstring'], config['images_container_name'])

