Python SDK for UploadThing
Project description
Installation
pip install uploadthing.py
Quickstart
Using UTApi
This is basically a 1:1 clone of the official TypeScript SDK
import asyncio, os
from uploadthing_py import UTApi
async def main():
utapi = UTApi(os.environ["UTAPI_KEY"])
# List the files in your app
res = await utapi.list_files()
print("List files:", res)
# Delete the first file from the list
key = res[0].key
res = await utapi.delete_file(key)
print("Delete file:", res)
# (TODO) Create a new file
# res = await utapi.upload_files()
if __name__ == "__main__":
asyncio.run(main())
Using FastAPI
You can use FastAPI like any of the JavaScript backend adapters.
Note: This is a work in progress and not yet ready for production use.
from fastapi import FastAPI, Request, Response
from uploadthing_py import (
create_uploadthing,
UploadThingRequestBody,
create_route_handler,
)
from fastapi.middleware.cors import CORSMiddleware
import os
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
f = create_uploadthing()
upload_router = {
"videoAndImage": f(
{
"image/png": {"max_file_size": "4MB"},
"image/heic": {"max_file_size": "16MB"},
}
)
.middleware(lambda req: {"user_id": req.headers["x-user-id"]})
.on_upload_complete(lambda file, metadata: print(f"Upload complete for {metadata['user_id']}"))
}
handlers = create_route_handler(
router=upload_router,
api_key=os.getenv("UPLOADTHING_SECRET"),
is_dev=os.getenv("ENVIRONMENT", "development") == "development",
)
@app.get("/api")
async def greeting():
return "Hello from FastAPI"
@app.get("/api/uploadthing")
async def ut_get():
return handlers["GET"]()
@app.post("/api/uploadthing")
async def ut_post(
request: Request,
response: Response,
body: UploadThingRequestBody,
):
return await handlers["POST"](
request=request,
response=response,
body=body,
)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
uploadthing_py-0.2.1.tar.gz
(7.9 kB
view hashes)
Built Distribution
Close
Hashes for uploadthing_py-0.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 57488be92dffbafb3c30512d026e14c68e58f1060d0e86b4eebce225c77bf326 |
|
MD5 | 8d7e5367303317091c775104dcc1bbfd |
|
BLAKE2b-256 | 40e193f2f0be6c68c5b1a8c7c1aa738aebf069d7eb0054c082e3a8aa27c3e1e8 |