File storage
List, upload, presign, get a download URL, delete. Each cell prints the request line (→) and the backend’s JSON reply, signed URLs masked.
A presigned upload is finished by PUT-ing the bytes to upload_url, then client.user.register_file(...).
Config
Add your settings to the repo .env (local) or Colab Secrets — this cell reads them with get(...):
INDOX_API_KEY (your ak_… key) and INDOX_BASE_URL (https://indox.org; the SDK adds /api/v1/… itself). A missing key stops here with its name.
import json, logging, re, sys, tempfile
from pathlib import Path
REPO = Path.cwd().parents[1]
sys.path[:0] = [str(REPO), str(REPO / "languages" / "python")]
try:
from google.colab import userdata
get = userdata.get
except ImportError:
from tools.env import require_env as get
from indox_client import Indox
logging.basicConfig(format="→ %(message)s", force=True)
logging.getLogger("urllib3.connectionpool").setLevel(logging.DEBUG)
def _mask(record):
record.msg = re.sub(r"/(public|sign)/[^/ ]+", r"/\1/***", record.getMessage())
record.args = ()
return True
logging.getLogger("urllib3.connectionpool").addFilter(_mask)
def show(data, limit=2500):
print(json.dumps(data, indent=2, default=str)[:limit])
client = Indox(api_key=get("INDOX_API_KEY"), base_url=get("INDOX_BASE_URL"))
start_credits = client.billing.credits_balance()["credit_amount"]
SAMPLES = REPO / "colab"
out = Path(tempfile.mkdtemp())
def masked(data):
return {k: "***" if k.endswith(("token", "url")) else v for k, v in data.items()}
png = SAMPLES / "image" / "samples" / "sample.png"files = client.user.list_files(page_size=3)
assert files["storage"]["limit_bytes"] > 0
show(files)stored = client.user.upload_file(png)
assert stored["file_size_bytes"] == png.stat().st_size
show(stored)presigned = client.user.presign_upload(file_name="notes.txt", content_type="text/plain", size_bytes=12)
assert presigned["upload_url"].startswith("https://") and presigned["s3_key"]
show(masked(presigned))urls = client.user.download_files({"file_ids": [stored["id"]]}).json()
assert urls["files"][0]["download_url"].startswith("http")
show([masked(f) for f in urls["files"]])deleted = client.user.delete_files({"file_ids": [stored["id"]]})
assert stored["id"] not in {f["id"] for f in client.user.list_files(page_size=50)["results"]}
show(deleted)Usage
now = client.billing.credits_balance()["credit_amount"]
print(f"credits used by this notebook: {start_credits - now} (balance {now})")
show(client.billing.usage_by_feature())