Skip to content

Instantly share code, notes, and snippets.

@Menziess
Last active June 15, 2024 14:56
Show Gist options
  • Save Menziess/e2fe3708e74e14885d83e9bd56cd5284 to your computer and use it in GitHub Desktop.
Save Menziess/e2fe3708e74e14885d83e9bd56cd5284 to your computer and use it in GitHub Desktop.
# Databricks notebook source
dbutils.widgets.removeAll()
def mount(source, mount_point, extra_configs):
try:
dbutils.fs.mount(
source=source,
mount_point=mount_point,
extra_configs=extra_configs)
dbutils.widgets.removeAll()
print(mount_point, 'mounted')
except Exception as e:
msg = 'already mounted'
if msg in str(e):
print(mount_point, msg)
else:
raise e
# COMMAND ----------
# MAGIC %md
# MAGIC
# MAGIC ## Utility for mounting blob (Credential Passthrough)
# COMMAND ----------
dbutils.widgets.text('container_name', 'datalake', 'Container Name')
dbutils.widgets.text('folder_name', '<proj>', 'Folder Name')
dbutils.widgets.text('storage_account_name', 'dnacdpacpdatalake', 'SA Name')
# COMMAND ----------
container_name = dbutils.widgets.get('container_name')
folder_name = dbutils.widgets.get('folder_name')
storage_account_name = dbutils.widgets.get('storage_account_name')
# COMMAND ----------
extra_configs = {
"fs.azure.account.auth.type": "CustomAccessToken",
"fs.azure.account.custom.token.provider.class": spark.conf.get("spark.databricks.passthrough.adls.gen2.tokenProviderClassName")
}
mount_point = f'/mnt/cdp_{folder_name}'
source = "abfss://{}@{}.blob.core.windows.net/{}".format(
container_name, storage_account_name, folder_name)
mount(source, mount_point, extra_configs)
# COMMAND ----------
# MAGIC %md
# MAGIC
# MAGIC ## Utility for mounting blob (SA Key)
# COMMAND ----------
dbutils.widgets.text('mount_point', '/mnt/?', 'Mount Point')
dbutils.widgets.text('container_name', '?', 'Container Name')
dbutils.widgets.text('storage_account_key', '?', 'SA Key')
dbutils.widgets.text('storage_account_name', '?', 'SA Name')
# COMMAND ----------
mount_point = dbutils.widgets.get('mount_point')
container_name = dbutils.widgets.get('container_name')
storage_account_key = dbutils.widgets.get('storage_account_key')
storage_account_name = dbutils.widgets.get('storage_account_name')
print("To mount container `{}@{}`, using key:".format(storage_account_name, container_name), end=' ')
for i in range(min(len(storage_account_key), 3)):
print(storage_account_key[i], end=' ')
print('...')
mount_point, container_name, storage_account_name
# COMMAND ----------
name = 'fs.azure.account.key.{}.blob.core.windows.net'.format(
storage_account_name)
source = "wasbs://{}@{}.blob.core.windows.net".format(
container_name, storage_account_name)
extra_configs = {name: storage_account_key}
mount(source, mount_point, extra_configs)
# COMMAND ----------
# MAGIC %md
# MAGIC
# MAGIC ### Utility for mounting blob (Service Principal)
# COMMAND ----------
dbutils.widgets.text('mount_point', '/mnt/?', 'Mount Point')
dbutils.widgets.text('container_name', '?', 'Container Name')
dbutils.widgets.text('storage_account_name', '?', 'SA Name')
dbutils.widgets.text('sp_client_id', '?', 'Service Principal Client ID')
dbutils.widgets.text('sp_client_secret_name', '?', 'Service Principal Client Secret Name (within keyvault)')
dbutils.widgets.text('tenant_id', 'Your Directory ID', 'Tenant ID')
# COMMAND ----------
mount_point = dbutils.widgets.get('mount_point')
container_name = dbutils.widgets.get('container_name')
storage_account_name = dbutils.widgets.get('storage_account_name')
sp_client_id = dbutils.widgets.get('sp_client_id')
sp_client_secret_name = dbutils.widgets.get('sp_client_secret_name')
tenant_id = dbutils.widgets.get('tenant_id')
# COMMAND ----------
source = "abfss://{}@{}.blob.core.windows.net/{}".format(
container_name, storage_account_name, folder_name)
extra_configs = {
"fs.azure.account.auth.type": "OAuth",
"fs.azure.account.oauth.provider.type": "org.apache.hadoop.fs.azurebfs.oauth2.ClientCredsTokenProvider",
"fs.azure.account.oauth2.client.id": sp_client_id,
"fs.azure.account.oauth2.client.secret": dbutils.secrets.get("keyvault", sp_client_secret_name),
"fs.azure.account.oauth2.client.endpoint": f"https://login.microsoftonline.com/{tenant_id}/oauth2/token"
}
mount(source, mount_point, extra_configs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment