Home / Blogs

How to Connect Amazon S3 Bucket with Django [2024]

Django
·

March 14, 2024

How to Connect Amazon S3 Bucket with Django

Amazon S3 (Simple Storage Service) is a widely used cloud storage service that allows you to store and retrieve data from anywhere on the web. Integrating Amazon S3 bucket with Django enables you to store media files, such as images, videos, and documents, in the cloud rather than on your server. This not only helps in reducing server load but also ensures the scalability and reliability of your Django application.

In this tutorial, we will walk through the process of connecting an Amazon S3 bucket with a Django application.

We will cover the necessary steps to set up an S3 bucket, configure Django settings, and use the boto3 library to interact with S3.

Prerequisites

Before proceeding, make sure you have the following:

  • Django is installed on your local machine.
  • An AWS account with access to Amazon S3.
  • boto3 and Django-storages installed in your Django project.

Step 1: Create an Amazon S3 Bucket

First, log in to your AWS Management Console and navigate to the S3 service. Create a new S3 bucket by clicking on the “Create bucket” button. Follow the prompts to configure your bucket, including its name, region, and any additional settings you may require.

Step 2: Install Required Packages

Install the boto3 and django-storages packages using pip:

pip install boto3
Pip install django-storages

Step 3: Configure Django Settings

In your Django project’s settings file (settings.py), add the following configurations to enable Amazon S3 storage:

INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    'storages',
]

STATIC_URL = 'static/'  
MEDIA_URL = 'media/'


AWS_ACCESS_KEY_ID = 'your_access_key_id'
AWS_SECRET_ACCESS_KEY = 'your_secret_access_key'/v'
AWS_STORAGE_BUCKET_NAME = 'your_bucket_name'
AWS_S3_REGION_NAME = 'your_bucket_region' 
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
AWS_S3_FILE_OVERWRITE = False

STORAGES = {

    # Media file (image) management  
    "default": {
        "BACKEND": "storages.backends.s3boto3.S3StaticStorage",
    },
   
    # CSS and JS file management
    "staticfiles": {
        "BACKEND": "storages.backends.s3boto3.S3StaticStorage",
    },
}

Add ‘storages’ in django INSTALLED_APPS replace ‘your_access_key_id’, ‘your_secret_access_key’, ‘your_bucket_name’, and ‘your_bucket_region’ with your actual AWS credentials and S3 bucket details and also add media and storages of both media and static files as specified in the code.

Step 4: Use S3 Storage in Django Models

Now that Django is configured to use Amazon S3 for file storage, you can use it in your models to store media files. For example, if you have a model named Document with a FileField for storing documents, you can define it like this:

# models.py
from django.db import models
class Document(models.Model):
    title = models.CharField(max_length=100)
    file = models.FileField(upload_to='documents/')

With the above setup, when you upload a document through a Django form, it will automatically be stored in your Amazon S3 bucket.

To read more about how to Implement Context Processors in Django, refer to our blog How to Implement Context Processors in Django

Conclusion

In this tutorial, we have covered the process of connecting an Amazon S3 bucket with a Django application. By following these steps, you can store media files securely in the cloud and ensure scalability and reliability for your Django projects. Make sure to keep your AWS credentials secure and never expose them publicly.
Integrating Amazon S3 with Django is a powerful way to manage your application’s media files and static assets, and it’s a recommended approach for production deployments. Experiment with different configurations and explore additional features provided by Amazon S3 to optimize your Django application’s performance and reliability.

Horilla Editorial Team Author

Horilla Editorial Team is a group of experienced writers and editors who are passionate about HR software. We have a deep understanding of the HR landscape and are committed to providing our readers with the most up-to-date and informative content. We have written extensively on a variety of HR software topics, including applicant tracking systems, performance management software, and payroll software etc. We are always looking for new ways to share our knowledge with the HR community. If you have a question about HR software, please don't hesitate to contact us.