Read & Write to Azure Blob Storage from Databricks

2 min read


In this post I’ll demonstrate how to Read & Write to Azure Blob Storage from within Databricks. Databricks can be either the Azure Databricks or the Community edition.

Cluster Details

Notebook Details

Notebook created with base language: Scala

Locate Azure Storage Details

Note that the following variables will be used throughout. These variables will need to be changed where necessary (Storage Account Name, Storage Account Key and Storage Account Source Container).

# Azure Storage Account Name
storage_account_name = "azurestorage"

# Azure Storage Account Key
storage_account_key = "1Vmkb3OQNgOoVI6MnhwerjhewrjhweFZVZ9w=="

# Azure Storage Account Source Container
container = "source"

# Set the configuration details to read/write
spark.conf.set("{0}".format(storage_account_name), storage_account_key)

Mount the filesystem & Blob Storage


# Check if file exists in mounted filesystem, if not create the file
if "Master.xlsm" not in [ for file in"/mnt/azurestorage")]:
   source = "wasbs://{0}@{1}".format(container, storage_account_name),
   mount_point = "/mnt/azurestorage",
   extra_configs = {"{0}".format(storage_account_name): storage_account_key}
  # Unmount filesystem if required
  # dbutils.fs.unmount("/mnt/azurestorage")

Check if files exist

# Check is all files exist"dbfs:/mnt/azurestorage")

Output command should be similar to this:

Install the xlrd library (optional)

I’m installing this library as I intend to manipulate Excel & CSV files. This is an optional Step.

%pip install xlrd

Read a file (optional)

To ensure that the filesystem and file is accessible – read a file.

df ="/mnt/azurestorage/b_Contacts.csv")

Write back to Azure Blob Storage container


// Write the file back to Azure Blob Storage
val df =
.option("inferSchema", "true")


// Save to the source container

// Display the output in a table

You must be logged in to post a comment.