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
from databricks.sdk import WorkspaceClient

w = WorkspaceClient()

# Read file into bytes
with open("local_file.csv", "rb") as f:
file_bytes = f.read()
binary_data = io.BytesIO(file_bytes)

# Specify volume path and upload
volume_path = "main.marketing.raw_files"

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

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

volume_file_path = f"/Volumes/{catalog}/{schema}/{volume_name}/local_file.csv"
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.

If you declare volume access in a Databricks Asset Bundle, resources.apps[*].resources[*].uc_securable may not grant USE_CATALOG and USE_SCHEMA on the parent catalog and schema (the app still needs them at runtime). As a temporary workaround until bundles can declare those parent grants, add the privileges manually, or see apps_grants_sync: an example Databricks App and Asset Bundle that wires experimental.scripts.postdeploy so parent privileges are applied after each databricks bundle deploy (copy its tools/ into your bundle or mirror the same pattern in databricks.yml).

Dependencies

requirements.txt
databricks-sdk
dash