If you’re building a Django web app, it’s essential to handle static assets and user-uploaded media files efficiently.

In a development environment, Django manages these files. In production, however, you’ll want to explore further options. Your user base may well grow significantly, and you might be dealing with much larger files, like audio and video.

AWS home page

The Amazon Web Services (AWS) Simple Storage Service (S3) Bucket is one alternative for hosting static and media files. By integrating S3 with Django, you can offload the burden of file management from your server, reduce load, and ensure faster, more reliable delivery of assets.

Step 1: Create an AWS Account

If you don’t have an AWS account, navigate to theAWS site, and create a new account.

New AWS accounts have free access to 5GB of S3 standard storage each month for a year.

AWS page to create an S3 bucket

Step 2: Create an S3 Bucket for Your Project

Step 3: Create an IAM User on AWS

AWS provides a service called IAM (Identity and Access Management). This allows you to create a separate account for a specific person or application that needs to interact with AWS services.

You can assign different levels of permissions to IAM users, representing individuals or applications that interact with the AWS services you have created. With IAM users, you can ensure that each user only has access to the resources they need and nothing more.

A form to create an S3 bucket

For security purposes, you should create an IAM user for your Django project to interact with your S3 bucket. Follow these steps to create an IAM user on AWS:

Step 4: Create an Access Key for Your IAM User

In AWS, an access key refers to credentials you can use to authenticate and securely access AWS resources programmatically. Your Django project must provide these credentials to access your S3 bucket.

The following steps will help you generate an access key for your project.

A form checkbox field for public access permission to S3 bucket

Step 5: Configure Your Django Project for S3 Bucket

To use your S3 bucket with a Django project, install these packages:

you may install these packages into yourPython virtual environmentwith Python’s Pip package manager by typing this command in your terminal:

A list of S3 buckets that have been created

Once you have successfully installed these packages, open yoursettings.pyfile and addboto3to the installed apps.

The last thing to do is configure your Django project to use the AWS S3 bucket. Here’s the general configuration to use:

Paste the above configuration in yoursettings.pyfile and replace the values accordingly. Replace yourAWS_ACCESS_KEY_IDandAWS_SECRET_ACCESS_KEYwith the access key and secret access key you copied or downloaded earlier. You should also change theAWS_STORAGE_BUCKET_NAMEandAWS_S3_REGION_NAMEto the names of your S3 bucket and region.

You can get the region name by navigating to your S3 bucket and copying the last values from theAWS regioncolumn.

Step 6: Test Your AWS Configuration

With the above steps complete, you should be ready to test your application by uploading files. The following code samples will upload files directly from the admin panel, but you’re free to upload yours from another place.

For context, you can have a model that looks like this:

Ensure you perform the necessary operations such as migrations, adding it to the admin panel, creating a view, and other things necessary for your project. Ensure you practiceDjango’s MVT principle.

Once you’re done, navigate to your admin panel or whatever form you have created for file upload, and upload an image file.

Navigate to your main site and confirm the image is there. If it is, right-click on the image and select theOpen image in new taboption. In the new tab containing the image, you’ll notice that the address bar references the S3 bucket you created earlier:

Another way to confirm your configuration is working is by navigating to your bucket on the AWS console. You will find your image there:

Step 7: Collect Static Files to Your S3 Bucket

So far, you have been able to upload media files to your S3 bucket; now, you need to upload your static files.

To do that, add these configurations to yoursettings.pyfile:

After that, open up yourCommand Line Interface (CLI)and run this command:

To confirm that everything works, open your S3 bucket in the AWS console. You will see a folder calledstatic.

Use AWS S3 Bucket for Much More

The possibilities of AWS S3 buckets are enormous! You should familiarize yourself with it and learn how to use S3 for other purposes, like hosting a static web application.

Knowing how to properly use S3 buckets will save you a lot of time and help you build a better product or solution. But you should also be aware that S3 might not be suitable for every case, so consider your project requirements before using it.