Skip to content

File Uploader

This guide explains how to use the upload function from the datallog lib to send files using a presigned URL.
The uploader performs validation, requests a presigned upload endpoint from the Datallog API, and then submits your file directly to the storage provider.

The result is a temporary public URL, valid for 30 days.


Overview

The upload flow works in two steps:

  1. Request a presigned URL from the Datallog backend
  2. Upload the file directly to the storage provider (e.g., S3)

This approach ensures fast uploads while keeping minimal load on Datallog’s core API.


Requirements

To use the uploader, you need:

  • A valid authorization token
  • A valid API key
  • A file provided as bytes or bytearray
  • Python 3.10+ and the requests library

You may provide the authorization token and API key directly when calling the function, or expose them as environment variables However, if you are running inside MWM cloud environment or with the sdk command "datallog run", these variables will be already set

bash
export datallog_user_auth_token="YOUR_TOKEN"
export datallog_x_api_key="YOUR_API_KEY"

How to Use the upload Function

Basic Upload Example

python
from datallog import uploader

with open("example.pdf", "rb") as f:
    file_bytes = f.read()

url = uploader.upload(
    filename="example.pdf",
    file=file_bytes,
    max_size=5  # MB
)

print("File hosted at:", url)

Explicit Authentication

python
url = uploader.upload(
    filename="image.png",
    file=image_bytes,
    auth_token="YOUR_TOKEN",
    x_api_key="YOUR_KEY",
)

Handling Errors

python

try:
    url = uploader.upload("data.csv", file_bytes, max_size=1)

except InvalidCredentialsError:
    print("Your credentials are invalid or missing.")

except APIError as e:
    print("Upload failed:", e)

except UploaderError as e:
    print("Input error:", e)

Returned Value

On success, the uploader returns a CloudFront URL where the file is accessible for 30 days.