Upload files
Learn how to upload files to WriftAI
Short on time?
If you just want to upload a file, skip ahead to Upload a file. To understand what happens under the hood, read on.
How uploads work
To get a file onto WriftAI, you create an upload, send the file's bytes to it, and complete it.
Upload steps
Create an upload
Call Create an upload with the file's exact size (up to 1 GB) and MIME type. The response contains the upload ID, the part URLs, and the part size. WriftAI decides how many parts the file needs, so you never have to choose.
Split the file into parts
Split the file into parts of the returned size. The last part may be smaller. There is one part for each URL.
Upload each part
Send each part with an HTTP PUT request to the URL at the same index. Set the Content-Type header to the file's MIME type and the Content-Length header to the part's size. If the response includes an ETag header, keep it. Retry a part if its upload fails. Parts are independent, so you can upload them concurrently to make better use of your bandwidth and finish large uploads faster.
Complete the upload
Call Complete an upload with the ETags, in the same order as the part URLs.
Complete uploads promptly
A pending upload expires 20 minutes after it is created and can no longer be completed. Create a new upload if that happens.
Downloading files
Completing an upload gives you a download URL for the file. The URL is signed, so no authentication is needed to download from it.
- CDN caching: if you are not close to where the file is stored, it is cached on a CDN for faster delivery. This is handled automatically.
- Range downloads: range requests are supported, so you can download a file in parallel parts. Every request counts toward the URL's rate limit, so keep the number of parallel requests within it.
- Deletion: files are automatically deleted 90 minutes after they are uploaded.
Keep the download URL
The URL returned for an uploaded file is the only way to reference it, so keep it. If it is lost or the file has been deleted, upload the file again.
We're working on persistent files that you can view, manage, and request new download URLs for.
Upload a file
WriftAI's official clients perform all of the steps above in a single call.
from pathlib import Path
from wriftai import Client, File
# Instantiate the WriftAI client
wriftai = Client()
# Upload a file, large files are split into parts and uploaded concurrently automatically
result = wriftai.files.upload(File(Path("report.pdf")))
print(result["download_url"])import { openAsBlob } from 'node:fs';
import { WriftAI, FileData } from 'wriftai';
// Instantiate the WriftAI client
const wriftai = new WriftAI();
// Upload a file, large files are split into parts and uploaded concurrently automatically
const result = await wriftai.files.upload(
new FileData(await openAsBlob('./report.pdf'), 'application/pdf'),
);
console.log(result.download_url);import (
"context"
"fmt"
"log"
"os"
"github.com/wriftai/wriftai-go"
)
func main() {
// Instantiate the WriftAI client
client := wriftai.NewClient()
file, err := os.Open("report.pdf")
if err != nil {
log.Fatal(err)
}
defer file.Close()
// Upload a file, large files are split into parts and uploaded concurrently automatically
result, err := client.Files.Upload(
context.TODO(),
wriftai.NewFile(file, ""),
nil, // default options
)
if err != nil {
log.Fatal(err)
}
fmt.Println(result.DownloadURL)
}# Upload a file, large files are split into parts and uploaded concurrently automatically
wriftai files upload report.pdf# Requires jq. Upload a file by performing each step yourself
set -e
FILE="report.pdf"
MIME_TYPE="application/pdf"
SIZE=$(wc -c < "$FILE" | tr -d ' ')
# 1. Create an upload
UPLOAD=$(curl -sSf "https://api.wrift.ai/v1/uploads" \
-H "Authorization: Bearer $WRIFTAI_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d "{\"size\": $SIZE, \"mime_type\": \"$MIME_TYPE\"}")
UPLOAD_ID=$(jq -r '.id' <<< "$UPLOAD")
PART_SIZE=$(jq -r '.part_size' <<< "$UPLOAD")
URLS=($(jq -r '.urls[]' <<< "$UPLOAD"))
# 2. Split the file into parts
DIR=$(mktemp -d)
trap 'rm -rf "$DIR"' EXIT
split -a 5 -b "$PART_SIZE" "$FILE" "$DIR/part-"
PARTS=("$DIR"/part-*)
# 3. Upload all parts concurrently, saving each response's headers
# (curl sets Content-Length from each part file automatically)
ARGS=()
for i in "${!URLS[@]}"; do
ARGS+=(--next -f --retry 3 -T "${PARTS[$i]}" \
-H "Content-Type: $MIME_TYPE" \
-D "$DIR/headers-$i" -o /dev/null "${URLS[$i]}")
done
curl --parallel "${ARGS[@]:1}" # --next separates transfers, skip the first one
# 4. Complete the upload with each part's ETag header, in part order
ETAGS=$(for i in "${!URLS[@]}"; do
grep -i '^etag:' "$DIR/headers-$i" | cut -d' ' -f2 | tr -d '\r'
done | jq -R . | jq -sc .)
curl -sSf "https://api.wrift.ai/v1/uploads/$UPLOAD_ID/complete" \
-H "Authorization: Bearer $WRIFTAI_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d "{\"etags\": $ETAGS}"More upload options
Clients support many more options, such as different file types and sources, MIME type overrides, and progress tracking. Refer to the reference of each client for all options.