Upload a file
This recipe uploads a file to a Unity Catalog volume using the Databricks SDK for Python.
Code snippet
import reflex as rx
import io
from databricks.sdk import WorkspaceClient
w = WorkspaceClient()
def check_upload_permissions(volume_name: str):
try:
volume = w.volumes.read(name=volume_name)
current_user = w.current_user.me()
grants = w.grants.get_effective(
securable_type="volume",
full_name=volume.full_name,
principal=current_user.user_name,
)
if not grants or not grants.privilege_assignments:
return "Insufficient permissions: No grants found."
for assignment in grants.privilege_assignments:
for privilege in assignment.privileges:
if privilege.privilege.value in ["ALL_PRIVILEGES", "WRITE_VOLUME"]:
return "Volume and permissions validated"
return "Insufficient permissions: Required privileges not found."
except Exception as e:
return f"Error: {e}"
class UploadFileState(rx.State):
upload_volume_path: str = ""
volume_check_success: bool = False
permission_result: str = ""
is_checking: bool = False
is_uploading: bool = False
uploaded_files: list[str] = []
@rx.event
async def check_volume_permissions(self):
self.is_checking = True
self.permission_result = ""
self.volume_check_success = False
yield
result = check_upload_permissions(self.upload_volume_path)
self.permission_result = result
if "validated" in result:
self.volume_check_success = True
self.is_checking = False
@rx.event
async def handle_upload(self, files: list[rx.UploadFile]):
if not files: return
self.is_uploading = True
yield
try:
parts = self.upload_volume_path.strip().split(".")
catalog, schema, volume_name = parts[0], parts[1], parts[2]
for file in files:
file_bytes = await file.read()
binary_data = io.BytesIO(file_bytes)
path = f"/Volumes/{catalog}/{schema}/{volume_name}/{file.filename}"
w.files.upload(path, binary_data, overwrite=True)
self.uploaded_files.append(file.filename)
yield rx.toast(f"Uploaded {file.filename} to {path}", level="success")
except Exception as e:
yield rx.toast(f"Upload failed: {e}", level="error")
finally:
self.is_uploading = False
Resources
Permissions
Your app service principal needs the following permissions:
USE CATALOGon the catalog of the volumeUSE SCHEMAon the schema of the volumeREAD VOLUMEandWRITE VOLUMEon 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
- Databricks SDK for Python -
databricks-sdk - Reflex -
reflex
databricks-sdk
reflex