Using AWS S3 Bucket in Django Rest Framework
Introduction
This is an attempt to revive a Django REST framework project. Years ago, I set up a bookstore management project. The bookstore is a DRF project that provides an API to manage various books for different categories of users. The project utilizes an AWS S3 bucket to store profile pictures when creating a user. Lately, I tried to spin up the project locally but encountered multiple errors.
In this article, I will cover the steps involved in getting the project to work; focusing mostly on how I set the AWS S3 bucket to work again.
Prerequisites
To understand this article, you will need a basic understanding of:
Django REST framework
AW3 S3 Bucket.
Here are the guides I used to set up the project. ¹ ²
Project setup
Installation from the git source
The first thing I did was to clone the project from Github, set up a virtual environment, and install the dependencies.
git clone git@github.com:mrbazzan/bookstore.git
python3 -m venv .venv
source .venv/bin/activate
pip3 install -r requirements.txt
Database setup
The initial attempt to start the project after installing from the git source raised an operational error because I hadn’t configured the database. Here is how I configured the database on a MacOS:
brew install postgresql@14 # if the PostgreSQL database is not installed
brew services start postgresql@14
Another attempt to start the project again raised another error: the required database, bookstore
could not be found. And so, I had to connect to the 'postgres' database and only then create the 'bookstore' database. The reason for initially connecting to the 'postgres' database was because psql
automatically tries to connect to a database whose name is the current user in the shell/process (in my case, bazzan
); so there is a need to explicitly instruct it to connect to another database, and then create the required database afterward. This was done with the command below:
psql -d postgres -c “CREATE DATABASE bookstore;”
The steps above were sufficient to start the Django development server. After that, I scanned the project's settings in book_store/settings.py
to determine the required AWS S3 environmental variables.
The next step was to generate values for the variables from my AWS account. I logged in to the account as a root user, then proceeded to the Users
section in the IAM
(Identity and Access Management) dashboard; the specific user was then selected from the users' tab to change its access key. In my case, the user's name was bazzan-books
, with AmazonS3FullAccess policy attached to allow full access to all buckets via the AWS management console.
I created a new access key (using this guide) because I couldn’t remember the old one. It is important to note that each access key has an associated secret access key. When the secret access key is lost or forgotten, it cannot be retrieved rather a new one is created and the old one is made inactive (or deleted).
The newly generated variables are added to the project. This is done with:
export AWS_ACCESS_KEY_ID=<insert_id_here>
export AWS_SECRET_ACCESS_KEY=<newly_generated_key_goes_here>
export AWS_S3_REGION_NAME=us-east-2
export AWS_STORAGE_BUCKET_NAME=django-books
Then, Django migrations are done, and the project is started with:
python manage.py migrate
python manage.py runserver
This was sufficient to get the project working again. The API endpoints can then be tested.
Lesson learned
Over-document your projects if possible. It always helps to have a "getting started guide" to help future self and other folks install the project from scratch
Take the time to learn the basics of your tools e.g. git, bash, etc.