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 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 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
reflex