There are three ways to access Azure storage blob from Databricks workspace (without Unity Catalog): (1) OAuth with service principal; (2) shared access signatures (SAS); and (3) account keys (not recommended for security reasons). This post will illustrate all the approaches, as well as common issues that block the connection. If you are using Unity Catalog, we have another post that explains how to set storage containers as external locations for data access.
OAuth with Service Principal
Let’s say we have a storage account that stores a JSON file in the container area under the path store/product.json. From Databricks workspace, we want to read this file and write another file in the container.

area/store/product.jsonThe first approach (and also what Databricks recommends) is using a service principal with role-based access control (RBAC). To that end, you need a service principal (SP) with appropriate IAM roles granted on the storage. Go to App Registrations in Azure Portal to locate or create the app (i.e., the identity or service principal) that you will use to connect to the storage, and note its Application (client) ID, Directory (tenant) ID, and client secret.


Next, the service principal should be granted an appropriate IAM role in the storage or the container. For read-only access, you need the Storage Blob Data Reader role. For read-write, you need the Storage Blob Data Contributor role. If the SP only needs access to a container but not the whole storage, the role can be granted at the container level.

Once the role is granted, we can navigate to Databricks workspace and set the Spark configs in the notebook:
spark.conf.set("fs.azure.account.auth.type.<storage-account>.dfs.core.windows.net", "OAuth")
spark.conf.set(
"fs.azure.account.oauth.provider.type.<storage-account>.dfs.core.windows.net",
"org.apache.hadoop.fs.azurebfs.oauth2.ClientCredsTokenProvider")
spark.conf.set("fs.azure.account.oauth2.client.id.<storage-account>.dfs.core.windows.net", "<application-id>")
spark.conf.set("fs.azure.account.oauth2.client.secret.<storage-account>.dfs.core.windows.net", "<client-secret>")
spark.conf.set(
"fs.azure.account.oauth2.client.endpoint.<storage-account>.dfs.core.windows.net",
"https://login.microsoftonline.com/<directory-id>/oauth2/token")<storage-account> is the name of the storage. <application-id>, <client-secret>, and <directory-id> are what you noted from the App Registration. Also, you can choose to store sensitive data in Secret Scope to avoid plain text in the code snippet. For example, you can store the client secret in Secret Scope named <scope-name> with the key name <key-name> and retrieve it as follows:
spark.conf.set(
"fs.azure.account.oauth2.client.secret.<storage-account>.dfs.core.windows.net",
dbutils.secrets.get(scope="<scope-name>", key="<key-name>"))After setting the configs, we can run the code below to test the read/write to the container:
# List the files in the folder of the container
dbutils.fs.ls("abfss://<container-name>@<storage-acccount>.dfs.core.windows.net/<folder-path>")
# Read the file. multiLine=True is for multi-line json
df = spark.read.json("abfss://<container-name>@<storage-acccount>.dfs.core.windows.net/<file-path>", multiLine=True)
df.head()
# Convert the dataframe to JSON and write it to the container
dbutils.fs.put("abfss://<container-name>@<storage-acccount>.dfs.core.windows.net/<file-path>", df.toJSON().collect()[0], overwrite=True)

Shared Access Signatures (SAS)
Shared Access Token (SAS) enables secure, limited access to resources within an Azure Storage account. It allows you to define permissions, expiration time, and access scope, without revealing the storage account keys. It can be generated and used at the storage, container, or file level. The screenshots below show how to generate a read/write SAS for the container area

Once we have the SAS, we can configure the Spark in the Databricks notebook as follows. SAS tokens for multiple storage accounts can be set in the same Spark session.
spark.conf.set("fs.azure.account.auth.type.<storage-account>.dfs.core.windows.net", "SAS")
spark.conf.set(
"fs.azure.sas.token.provider.type.<storage-account>.dfs.core.windows.net",
"org.apache.hadoop.fs.azurebfs.sas.FixedSASTokenProvider")
spark.conf.set("fs.azure.sas.fixed.token.<storage-account>.dfs.core.windows.net", "<sas-token>"))With the SAS token configured, we can perform the same read/write testing on the container as in the previous section.

Account keys
Storage Account Key is a secret key that provides full administrative access to all resources in an Azure Storage account. Since the other two alternatives (RBAC and SAS tokens) offer better security, granular control, and limited access, you should only use account keys for quick, non-prod testing and in sandbox environments. That said, you can retrieve the storage keys and configure the Spark session in Databricks notebooks as follows.

spark.conf.set(
"fs.azure.account.key.<storage-account>.dfs.core.windows.net",
"<storage-account-access-key>")
Access Troubleshooting
Network
If the storage only allows selected networks, you may see 403 errors when testing access.

To resolve the issue, you must add the public IP or the VNets used by Databricks (more precisely, by the compute) to the storage. You can locate the VNets used by the Databricks (private subnet and public subnet) from the Overview in Azure Portal.


Permission
Permission errors are usually self-explanatory. For example, if you have only read access but try to write to the container, you may see errors like below.

To unblock, simply check if the service principal has an appropriate role (e.g., Storage Blob Data Contributor), or generate a new SAS token that has the correct permissions granted.
References:
