How to set up AWS S3 in Django?

How to set up AWS S3 in Django?

AWS S3 in Django

Gaurav Sharma's photo
Gaurav Sharma
·May 31, 2022·

1 min read

Subscribe to our newsletter and never miss any upcoming articles

Play this article

In Django, we can use AWS S3 for two common things:

  • For user uploaded files/images
  • For Django static files

Before we cover both the use cases, let’s install a third party package, we’ll be using this third party package named django-storages in this blog.

Let’s install:

pip install django-storages

For user uploaded files/images:

Put this code in your project’s settings.py file:

DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

# AWS Credentials 

AWS_ACCESS_KEY_ID = "PASTE IT HERE"

AWS_SECRET_ACCESS_KEY = "PASTE IT HERE"

AWS_STORAGE_BUCKET_NAME = "PASTE IT HERE"

For Django static files:

For storing the Django static files in S3 bucket, the procedure is same as above, you just have to add this extra config in your settings.py file.

STATICFILES_STORAGE = 'storages.backends.s3boto3.S3StaticStorage'

And you are good to go now, run the collectstatic command.

Any doubts? Write it down in the comments.

For more such crispy blogs daily, follow Dev.Junction, subscribe to our newsletter and get notified.

 
Share this