Python Tutorial
#
Building a Simple Image Server with Deta Drive#
SetupTo get started, create a directory image-server
and change the current directory into it.
Before we begin, let's install all the necessary dependencies for this project. Create a requirements.txt
with the following lines:
If you are using Deta Drive within a Deta Micro, you should ignore uvicorn
, but you must include deta
in your requirements.txt
file to install the lastest sdk version, other than that it won't work.
We are using FastAPI
to build our simple image server, and python-multipart
allows us access the uploaded files.
Run the following command to install the dependencies.
To configure the app, import the dependencies and instantiate drive in main.py
We have everything we need to 🚀
#
Uploading ImagesFirst, we need to render a HTML snippet to display the file upload interface.
We'll expose a function that renders the HTML snippet on the base route /
We are simply rendering a form that sends a HTTP POST
request to the route /upload
with file data.
Let's complete file upload by creating a function to handle /upload
Thanks to the amazing tools from FastAPI, we can simply wrap the input around UploadFile
and File
to access the image data. We can retrieve the name as well as bytes from file
and store it in Drive.
#
Downloading imagesTo download images, we can simply use drive.get(name)
If we tie a GET
request to the /download
path with a param giving a name (i.e /download/space.png
), we can return the image over HTTP.
You can learn more about StreamingResponse
here.
#
Running the serverTo run the server locally, navigate to the terminal in the project directory (image-server
) and run the following command:
Your image server is now ready! You can interact with it at /
and check it out!


#
IssuesIf you run into any issues, consider reporting them in our Github Discussions.