Dilnaka Python SDK
Python SDK for Dilnaka Storage. It asks the Dilnaka API for presigned
storage URLs, sends file bytes straight to storage, and completes the
upload. It never receives storage credentials.
Files smaller than multipart_threshold (16 MiB by default) go in one
presigned PUT, streamed from disk. Larger files use a resumable multipart
upload: parts are sent in parallel, retried on failure, and an interrupted
upload is continued later instead of starting over.
Install
pip install dilnaka
Requires Python 3.14 or newer.
Configure
Every setting resolves in this order: explicit constructor argument, then
the process environment, then .env, then the default.
| Setting (constructor argument) |
Environment |
Default |
Bounds |
api_key |
DILNAKA_API_KEY |
none (required) |
starts dlk_dev_ or dlk_live_ |
timeout: seconds for each JSON API call |
DILNAKA_TIMEOUT |
60 |
> 0 |
upload_timeout: seconds for each storage PUT (the single PUT or one part) |
DILNAKA_UPLOAD_TIMEOUT |
300 |
> 0 |
multipart_threshold: bytes at which multipart starts |
DILNAKA_MULTIPART_THRESHOLD |
16777216 (16 MiB) |
> 0 |
upload_concurrency: parts in flight at once |
DILNAKA_UPLOAD_CONCURRENCY |
4 |
1 to 16 |
max_retries: extra attempts for a JSON call |
DILNAKA_MAX_RETRIES |
3 |
0 to 10 |
part_retries: extra attempts for a part PUT |
DILNAKA_PART_RETRIES |
5 |
0 to 10 |
DILNAKA_API_KEY=dlk_dev_your_api_key_here
DILNAKA_TIMEOUT=60
DILNAKA_UPLOAD_TIMEOUT=300
DILNAKA_MULTIPART_THRESHOLD=16777216
DILNAKA_UPLOAD_CONCURRENCY=4
DILNAKA_MAX_RETRIES=3
DILNAKA_PART_RETRIES=5
from dilnaka import Dilnaka
client = Dilnaka() # everything from env / .env
client = Dilnaka(api_key="dlk_dev_...", part_retries=8)
The base URL is fixed to https://dilnaka.storage.tsnc.tech.
Retries. JSON calls retry 429 (honouring Retry-After) and
502/503/504, nothing else. A part PUT is retried up to
part_retries times after network errors, 408, 429 and 5xx, waiting
1, 2, 4, 8, 16 ... seconds (at most 30, Retry-After honoured). A 403
(an expired URL) presigns that part again and retries. Any other 4xx
fails the part at once. Each backoff wait is shortened by a random factor
between 0.5 and 1 so clients that failed together do not retry together; a
Retry-After is always waited in full.
Upload
uploaded = client.upload("./report.pdf")
print(uploaded.id, uploaded.key, uploaded.status)
upload(file_path, **options) takes the local file's path and these
options, all keyword-only:
| Option |
Meaning |
folder |
default "uploads" |
filename |
stored name; default the path's base name |
content_type |
default guessed from the name, else application/octet-stream |
metadata |
a JSON object stored with the file |
multipart_threshold |
per-call override |
upload_timeout |
per-call override of the transfer timeout |
on_progress |
called with an UploadProgress after each part lands, or when the single PUT lands |
on_session_started |
multipart only: called with the MultipartUpload before any part is sent |
cancel_event |
a threading.Event; set it to stop the upload |
uploaded = client.upload(
"./course-bundle.zip",
folder="courses",
filename="bundle-2026.zip",
metadata={"course": "intro"},
)
Progress
def show(progress):
percent = 100 * progress.uploaded_bytes / max(progress.total_bytes, 1)
print(f"{progress.file_id}: {percent:.0f}% (part {progress.part_number})")
client.upload("./video.mp4", on_progress=show)
UploadProgress has file_id, uploaded_bytes, total_bytes and
part_number (None for a single PUT). Progress is reported from the
calling thread, one event at a time.
Cancel
import threading
from dilnaka import UploadCancelledError
cancel = threading.Event()
# From another thread, a signal handler or a UI button: cancel.set()
try:
client.upload("./video.mp4", cancel_event=cancel)
except UploadCancelledError as error:
print("cancelled", error.file_id) # None if the upload never started
The event is checked before each part and between retries. A cancelled
multipart upload is not discarded: it stays resumable.
Resume
A transfer failure never aborts a multipart upload. When a part keeps
failing, upload raises UploadInterruptedError with the file_id; the
parts already in storage stay there. Persist the file_id as soon as the
upload exists, from on_session_started, so you can resume even after a
crash:
from dilnaka import Dilnaka, UploadInterruptedError
client = Dilnaka()
path = "./video.mp4"
def remember(upload):
save_pending_upload(path, upload.file_id) # your storage: a file, a database row
try:
uploaded = client.upload(path, on_session_started=remember)
except UploadInterruptedError as error:
print("interrupted; resume later:", error.file_id) # the saved file_id
# Later, or in a new process:
uploaded = client.resume_upload(path, load_pending_upload(path))
resume_upload(file_path, file_id, *, upload_timeout=None, on_progress=None, cancel_event=None) asks the server what it holds, then:
- an upload that already finished returns its file without sending bytes;
- a failed upload raises
UploadFailedError with its reason;
- a pending upload checks the local file is still the same size (otherwise
UploadFailedError, "the file changed") and that the upload's part
layout fits it, sends only the missing parts,
and completes with the parts already stored plus the new ones. Progress
starts at the bytes already uploaded.
An open multipart upload stays resumable for 24 hours after its last
activity, and at most 7 days after it was created;
MultipartUpload.session_expires_at says when. Only
abort_multipart_upload(file_id) discards an upload.
Files
for file in client.list_files(limit=50): # 100 by default, 1 to 500
print(file.id, file.status, file.key)
file = client.get_file("file_123")
access = client.get_file_access_url("file_123", expires_in=600)
print(access.url, access.expires_in, access.is_temporary)
deleted = client.delete_file("file_123")
print(deleted.deleted, deleted.file_id, deleted.file.status)
Low-level calls
Each makes one HTTP call:
| Method |
Returns |
create_presigned_upload(filename, content_type, size, folder="uploads", metadata=None) |
PresignedUpload |
complete_upload(file_id) |
UploadedFile |
create_multipart_upload(filename, content_type, size, folder="uploads", metadata=None) |
MultipartUpload |
presign_multipart_parts(file_id, part_numbers) (at most 500) |
PresignedParts |
get_multipart_upload_status(file_id) |
UploadStatus |
complete_multipart_upload(file_id, parts) with MultipartUploadParts |
UploadedFile |
abort_multipart_upload(file_id) |
UploadedFile (status failed) |
list_files(limit=None) |
list[UploadedFile] |
get_file(file_id) |
UploadedFile |
get_file_access_url(file_id, expires_in=None) |
FileAccessUrl |
delete_file(file_id) |
DeletedFile |
All result types are frozen dataclasses with snake_case fields.
Errors
Every error the SDK defines derives from DilnakaError. An invalid
argument (an empty file_id, a non-positive upload_timeout, an
out-of-range limit, ...) raises the built-in ValueError instead.
| Error |
When |
DilnakaConfigError |
a setting is missing or out of bounds |
DilnakaAPIError |
the API or storage answered an error, or a response lacks a required field; has message, status_code, response_body |
DilnakaHTTPError |
a request could not be sent or answered (connection, DNS, TLS, timeout) |
DilnakaFileNotFoundError |
the local path is missing or not a file (also a FileNotFoundError) |
UploadInterruptedError |
a multipart transfer stopped; has file_id and cause, resume it |
UploadCancelledError |
cancel_event was set; has file_id (None before the upload existed) |
UploadFailedError |
the upload failed or the local file changed; has file_id and reason |
Security model
The SDK never receives storage credentials, only short-lived presigned URLs.
Dilnaka Storage validates the API key and its scopes, the file, and every
completion.