Easy Download Links from Databricks Notebooks using FileStore
Summary
- Databricks FileStore allows you to store files that are accessible via download links
- You can programmatically generate download URLs for files in FileStore
- While FileStore works, using external cloud storage like Azure Blob Storage or AWS S3 is generally recommended for file sharing
Detail
Databricks FileStore provides a convenient way to store files that you want to make available for download. By copying files into the special /FileStore
directory in DBFS, you can easily generate URLs that allow users to download those files directly from rendered notebooks.
This code snippet assumes you have already generated a file, like an Excel spreadsheet, and saved it to a temporary location like /tmp
. Adjust the code based on your specific use case.
Here’s how you can create a download link for a file using FileStore in a Python notebook:
# File paths
= "/tmp/output_data.xlsx"
temp_file_path = "/FileStore/output_data.xlsx"
filestore_path
# Code to generate the Excel file and save to temp_file_path
# goes here...
# Copy the file from temp directory to FileStore
f"file:{temp_file_path}", filestore_path)
dbutils.fs.cp(
# Get the URL parameters
= spark.conf.get("spark.databricks.workspaceUrl")
workspace_url
# Generate the download URL
= f"https://{workspace_url}/files/output_data.xlsx"
download_url
# Render the download link using displayHTML
f"<a href='{download_url}'>Click here to download the Excel file</a>") displayHTML(
The key steps are:
- Copy the file into FileStore using
dbutils.fs.cp
- Retrieve the workspace URL from the Spark context
- Construct the download URL
- Render the download link using
displayHTML
When a user clicks the rendered download link, it will prompt their browser to download the file directly from FileStore.
Reference
For more details on the FileStore and other Databricks file system concepts, check out: