Towards AI

The leading AI community and content platform focused on making AI accessible to all. Check out our new course platform: https://academy.towardsai.net/courses/beginner-to-advanced-llm-dev

Follow publication

How to List, Read, Upload, and Delete Files in Azure Blob Storage With Python.

Prithivee Ramalingam
Towards AI
Published in
7 min readMar 29, 2023

Introduction

Image by Author

1. Perform operations with Connection String

1.1 Installation

pip install azure-storage-blob

1.2 Creating a Container Client

from azure.storage.blob import BlobServiceClient, BlobClient

def get_details():
connection_string = 'DefaultEndpointsProtocol=https;AccountName=your_account_name;AccountKey=your_account_key'
container_name = 'your_container_name'
return connection_string, container_name

def get_clients_with_connection_string():
connection_string, container_name = get_details()
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
container_client = blob_service_client.get_container_client(container_name)
return container_client

1.3 Read from blob with Connection String

def read_from_blob(blob_file_path):
container_client = get_clients_with_connection_string()
blob_client = container_client.get_blob_client(blob_file_path)
byte_data= blob_client.download_blob().readall()
return byte_data

def save_to_local_system(byte_file, filename):
with open(f'{filename}', 'wb') as f:
f.write(byte_file)
blob_file_path = "your_blob_file_path"
filename_in_local = "your_local_filename_for_saving"

byte_data = read_from_blob(blob_file_path)

# Optional
save_to_local_system(byte_data, filename_in_local)

1.4 List the files in a folder using Connection String

def list_files_in_blob(folder_name):
container_client = get_clients_with_connection_string()
file_ls = [file['name'] for file in list(container_client.list_blobs(name_starts_with=folder_name))]
return file_ls
folder_name = "your_folder_name"

file_ls = list_files_in_blob(folder_name)

1.5 Upload to blob storage using Connection String

def read_from_local_system(local_filename):
with open(f'{local_filename}', 'rb') as f:
binary_content = f.read()
return binary_content

def upload_to_blob_with_connection_string(file_name,blob_name):
connection_string, container_name = get_details()
blob = BlobClient.from_connection_string(connection_string, container_name=container_name, blob_name=blob_name)
with open(file_name, "rb") as data:
blob.upload_blob(data)
local_filename = "your_local_filename"
blob_name = "your_blob_name"

byte_data = read_from_local_system(local_filename)
upload_to_blob_with_connection_string(file_name,blob_name)

1.6 Delete files with Connection String

# If the blob has any associated snapshots, you must delete all of its snapshots to delete the blob.
# The following example deletes a blob and its snapshots:

# To delete only the snapshots and not the blob itself, you can pass the parameter delete_snapshots="only".
# To delete just the blob we can call the functions without any parameters

def delete_blob(blob_name):
connection_string, container_name = get_details()
blob = BlobClient.from_connection_string(connection_string, container_name=container_name, blob_name=blob_name)
blob.delete_blob(delete_snapshots="include")
print(f"Deleted {blob_name}")

blob_name = "your_blob_name"

delete_blob(blob_name)

2. Perform operations with SAS URL

2.1 Installation

pip install azure-storage-blob

2.2 Get blob client with SAS URL

from azure.storage.blob import  BlobClient

def get_blob_client_with_sas_url(blob_name,blob_container_name):
sas_url = "https://youraccount.blob.core.windows.net/yourcontainer?yourSASToken"
blob_client = BlobClient.from_blob_url(sas_url)
return blob_client

2.3 Read from blob using SAS URL

def read_from_blob_with_sas_url(blob_name,blob_container_name):
blob_client = get_blob_client_with_sas_url(blob_name,blob_container_name)
byte_data = blob_client.download_blob().readall()
return byte_data

def save_to_local(byte_file, filename):
with open(f'{filename}', 'wb') as f:
f.write(byte_file)
blob_name = "your_blob_name"
blob_container_name = "your_blob_container_name"
local_filename = "your_local_filename_for_saving"

byte_data = read_from_blob_with_sas_url(blob_name,blob_container_name)

# Optional
save_to_local(byte_data, local_filename)

2.4 List the files in a folder using SAS URL

from azure.storage.blob import ContainerClient


def get_container_client_with_sas_url(blob_container_name):
sas_url = "https://youraccount.blob.core.windows.net/yourcontainer?yourSASToken"
container_client = ContainerClient.from_container_url(sas_url)
return container_client

def list_files_in_blob_with_sas_url(blob_container_name,blob_name):
container_client = get_container_client_with_sas_url(blob_container_name)
file_ls = [file['name'] for file in list(container_client.list_blobs(name_starts_with=blob_name))]
return file_ls

2.5 Upload to blob storage using SAS URL

def read_from_local_system(filename):
with open(f'{filename}', 'rb') as f:
binary_content = f.read()
return binary_content

def upload_to_blob_with_sas_url(blob_name,blob_container_name,byte_data):
blob_client = get_blob_client_with_sas_url(blob_name,blob_container_name)
blob_client.upload_blob(byte_data,overwrite=True)
print("Uploaded the blob",blob_name)
blob_name = "your_blob_name"
blob_container_name = "your_blob_container_name"
local_filename = "your_local_filename"

byte_data = read_from_local_system(local_filename)
upload_to_blob_with_sas_url(blob_name,blob_container_name,byte_data)

2.6 Delete files with SAS URL

def delete_blob(blob_name,blob_container_name): 
blob_client = get_blob_client_with_sas_url(blob_name,blob_container_name)
blob_client.delete_blob(delete_snapshots="include")
print(f"Deleted {blob_name}")
blob_name = "your_blob_name"
blob_container_name = "your_blob_container_name"

delete_blob(blob_name,blob_container_name)

3. Take a backup of blobs (recursively)

from azure.storage.blob import BlobServiceClient
import os

def get_clients_with_connection_string(connection_string,container_name):
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
container_client = blob_service_client.get_container_client(container_name)
return container_client

def read_from_blob(connection_string, container_name,file_path):
container_client = get_clients_with_connection_string(connection_string,container_name)
blob_client = container_client.get_blob_client(file_path)
xml_string = blob_client.download_blob().readall()
return xml_string

def save_to_local(byte_file, filename):
with open(f'{filename}', 'wb') as f:
f.write(byte_file)

def list_files_in_blob(blob_folder,connection_string,container_name):
container_client = get_clients_with_connection_string(connection_string,container_name)
mapping_ls = [file['name'] for file in list(container_client.list_blobs(name_starts_with=blob_folder))]
return mapping_ls

def create_backup(connection_string,container_name,blob_folder):
ls = list_files_in_blob(blob_folder,connection_string,container_name)
for file in ls:
if '/'.join(file.split("/")[:len(blob_folder.split("/"))]) == blob_folder:
folder_name = '/'.join(file.split("/")[:-1])
if os.path.exists(folder_name) == False:
os.makedirs(folder_name)
byte_file = read_from_blob(connection_string,container_name,file)
save_to_local(byte_file, file)
print('Saved',file)
container_name = 'your_container_name'
blob_folder = 'your_blob_name'
connection_string = 'DefaultEndpointsProtocol=https;AccountName=your_account_name;AccountKey=your_account_key'

create_backup(connection_string,container_name,blob_folder)

4. Copy files from one account to another

from azure.storage.blob import BlobServiceClient
from azure.storage.blob import BlobClient

def get_clients_with_connection_string(connection_string,container_name):
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
container_client = blob_service_client.get_container_client(container_name)
return container_client

def read_from_blob(source_connection_string, source_container_name,blob_path):
container_client = get_clients_with_connection_string(source_connection_string,source_container_name)
blob_client = container_client.get_blob_client(blob_path)
xml_string = blob_client.download_blob().readall()
return xml_string

def upload_to_blob(target_connection_string,target_container_name,file_path,byte_file):
blob = BlobClient.from_connection_string(target_connection_string, container_name=target_container_name, blob_name=file_path)
blob.upload_blob(byte_file,overwrite=True)
blob.close()
print('Uploaded',file_path)

def list_files_in_blob(source_connection_string,source_container_name,source_blob_folder):
container_client = get_clients_with_connection_string(source_connection_string,source_container_name)
file_ls = [file['name'] for file in list(container_client.list_blobs(name_starts_with=source_blob_folder))]
return file_ls

def perform_file_copy(source_connection_string,source_container_name,source_blob_folder,target_connection_string,target_container_name,target_blob_folder):
file_ls = list_files_in_blob(source_connection_string,source_container_name,source_blob_folder)
for blob_path in file_ls:
if '/'.join(blob_path.split("/")[:len(source_blob_folder.split('/'))]) == source_blob_folder:
byte_file = read_from_blob(source_connection_string, source_container_name,blob_path)
blob_path = f'{target_blob_folder}/{blob_path}'
upload_to_blob(target_connection_string,target_container_name,blob_path,byte_file)
source_connection_string = 'your_source_connection_string'
source_container_name = 'your_source_container_name'
source_blob_folder = 'your_source_blob_name'

target_connection_string = 'your_target_connection_string'
target_container_name = 'your_target_container_name'
target_blob_folder = 'your_target_blob_name'

perform_file_copy(source_connection_string,source_container_name,source_blob_folder,target_connection_string,target_container_name,target_blob_folder)

Conclusion:

Want to Connect?

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Published in Towards AI

The leading AI community and content platform focused on making AI accessible to all. Check out our new course platform: https://academy.towardsai.net/courses/beginner-to-advanced-llm-dev

Written by Prithivee Ramalingam

I am a Machine Learning Engineer, currently working on GenAI, NLP and MLOps.

No responses yet

Write a response