Skip to main content

Upload a file

This recipe uploads a file to a Unity Catalog volume using the Databricks SDK for Python.

Code snippet

app.py
import io
import streamlit as st
from databricks.sdk import WorkspaceClient

w = WorkspaceClient()

uploaded_file = st.file_uploader(label="Select file")

upload_volume_path = st.text_input(
label="Specify a three-level Unity Catalog volume name (catalog.schema.volume_name)",
placeholder="main.marketing.raw_files",
)

if st.button("Save changes"):
file_bytes = uploaded_file.read()
binary_data = io.BytesIO(file_bytes)
file_name = uploaded_file.name

parts = upload_volume_path.strip().split(".")

catalog = parts[0]
schema = parts[1]
volume_name = parts[2]

volume_file_path = f"/Volumes/{catalog}/{schema}/{volume_name}/{file_name}"
w.files.upload(volume_file_path, binary_data, overwrite=True)

Resources

Permissions

Your app service principal needs the following permissions:

  • USE CATALOG on the catalog of the volume
  • USE SCHEMA on the schema of the volume
  • READ VOLUME and WRITE VOLUME on the volume

See Privileges required for volume operations for more information.

Dependencies

requirements.txt
databricks-sdk
streamlit