Sub topic 10
Uploading with the Databricks CLI, SDK and Files API
This chapter assumes you can create a volume, know the /Volumes/<catalog>/<schema>/<volume>/<path> format, and have WRITE VOLUME on the target volume. You also need the Databricks CLI installed and authenticated, for example with databricks auth login.
When to use programmatic upload
The workspace UI is fine for a single file. Scripts, CI jobs, and anything repeated should use one of three tools:
- The Databricks CLI, for shell scripts and quick transfers.
- The Databricks SDK, for upload logic inside Python programs.
- The Files API, the REST endpoint that the CLI and SDK both call.
All three need the same privileges as the UI. The identity you authenticate as must hold WRITE VOLUME to upload and READ VOLUME to list or download.
Uploading with the CLI
The databricks fs commands address volume paths with a dbfs: prefix. The prefix is part of the CLI's path syntax. The volume path itself is unchanged.
databricks fs cp ./sales.csv dbfs:/Volumes/main/default/landing/sales.csv
databricks fs ls dbfs:/Volumes/main/default/landing/The first command copies a local file into the volume. The second lists the volume to confirm the file arrived. Without the dbfs: prefix, the CLI treats the path as a local one and the copy fails or goes to the wrong place.
Uploading with the Python SDK
The SDK's files client wraps the Files API. Install it with pip install databricks-sdk. It reads credentials from the same environment or profile as the CLI.
import io
from databricks.sdk import WorkspaceClient
w = WorkspaceClient()
volume_path = "/Volumes/main/default/landing/report.txt"
w.files.upload(volume_path, io.BytesIO(b"hello from the SDK"), overwrite=True)
for entry in w.files.list_directory_contents("/Volumes/main/default/landing/"):
print(entry.path, entry.file_size)upload takes the destination path and a binary file-like object. Set overwrite=True to replace an existing file. By default, uploading to an existing path fails.
How the pieces connect
The CLI and SDK send HTTP requests to the Files API. The API checks your privileges in Unity Catalog and writes to the volume's storage.
Calling the Files API directly
Use the REST endpoint when you cannot install the CLI or SDK. The path is part of the URL, and the request body is the raw file content.
curl --request PUT \
"https://$DATABRICKS_HOST/api/2.0/fs/files/Volumes/main/default/landing/sales.csv?overwrite=true" \
--header "Authorization: Bearer $DATABRICKS_TOKEN" \
--data-binary @sales.csvUse --data-binary rather than --data. Plain --data can alter newlines in the file.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Permission denied | Missing `WRITE VOLUME` | Ask the volume owner to grant it |
| File not found on a path that looks right | Missing `dbfs:` prefix in the CLI | Add `dbfs:` before `/Volumes` |
| Upload fails because the file exists | `overwrite` not set | Use `--overwrite` in the CLI or `overwrite=True` in the SDK |
| Volume not found | Wrong catalog, schema, or volume name | Check each path segment against Catalog Explorer |
Exercise
A teammate runs databricks fs cp ./data.csv /Volumes/main/default/landing/data.csv and the command does not put the file in the volume. What is the most likely mistake, and what is the corrected command?
Show answer
The path is missing the dbfs: prefix, so the CLI treats it as a local path. The corrected command is databricks fs cp ./data.csv dbfs:/Volumes/main/default/landing/data.csv.
Exercise
Your Python job uploads a file with w.files.upload(path, data) and fails on the second run with an error that the file already exists. What change fixes it?
Show answer
Pass overwrite=True, as in w.files.upload(path, data, overwrite=True).