How to use AWS S3 Bucket to store static and media files in Django
Amazon Web Services (AWS) Simple Storage Service (S3) Bucket is an alternative for storing static and multimedia files. By integrating S3 with Django, you can offload the file management burden on your server, offloading, and ensuring faster, more reliable content delivery.
Step 1: Create an AWS account
If you don't have an account, go to the AWS site to create a new account.
New AWS accounts get free access to 5GB of S3 standard storage per month for one year.
Step 2: Create S3 Bucket for the project
- After creating an AWS account, log in and find S3 in the search bar at the top, then click the first option.
- Then you will see a new page, click Create bucket button .
- Next, name your S3 bucket . You can keep the default configuration.
- Scroll down to Block Public Access settings for this bucket , uncheck Block all public access and confirm the warning that appears.
- Once done, click the Create bucket button . It will take you to a page showing a list of the S3 buckets that have been created.
Step 3: Create IAM User on AWS
AWS provides a service called IAM (Identity and Access Management). This allows you to create a separate account for specific people or applications that need to interact with AWS services.
You can assign different levels of permissions to IAM users who represent individuals or applications that interact with created AWS services. With IAM users, you can ensure each user only has access to the resources they need and no more.
For security purposes, you should create an IAM user for your Django project to interact with the S3 bucket. Follow these steps to create an IAM user on AWS:
- In the search bar, type IAM and click the first option. A new page appears:
- On the left hand side of the IAM page, select Users, then continue to click the Add users button . It will open another page to fill in the information.
- Start by entering a name for the IAM user and click the Next button at the bottom.
- On the next page, you have to select the permission levels for the IAM user. Follow these steps:
- First, select Attach policies directly from Permissions options .
- Next, define the authorization policy for the IAM user. This action determines what an IAM user can and cannot do. Since you want the Django app to download and upload files, you should give it full access to the S3 bucket.
- In the Permissions policies section , you should find & select S3FullAccess . Then, click Next .
- Next, review the policies for the IAM user and click the Create user button to create the IAM user.
Step 4: Generate an access key for the IAM . user
In AWS, a credential-only access key you can use to programmatically authenticate and access AWS resources. Your Django project must provide these credentials to access the S3 bucket.
The following steps will help you create an access key for the project:
- After creating the IAM user, you will receive a prompt to view user. You can view the user by clicking User name.
- Next, select the Security credentials tab > scroll down to find Access keys and select Create access key .
- You need to choose a use case for the access, so that AWS can suggest suitable alternatives. It does not affect the access key. Feel free to click an option like Hird-party service or Local code and confirm the warning. Then, click the Next button .
- On the next page, enter a description tag for the access key and click the Create access key button .
- After generating the access key, you can copy the credentials or download them as a CSV file. Either way, make sure this data is safe and secure.
Step 5: Configure Django project for S3 Bucket
To use S3 buckets with Django projects, install the following packages:
- django-storages helps you define a storage backend for files.
- boto3 is the AWS Software Development Kit (SDK) that helps Python projects interact with AWS.
You can install these packages into a virtual Python environment with Python's Pip package manager with the following command in the terminal:
pip install django-storages boto3
After successfully installing these packages, open the settings.py file and add boto3 to the installed app.
Finally, configure the Django project to use the AWS S3 bucket. Here is the common configuration:
AWS_ACCESS_KEY_ID = 'AWS_ACCESS_KEY_ID ' AWS_SECRET_ACCESS_KEY = 'AWS_SECRET_ACCESS_KEY' AWS_STORAGE_BUCKET_NAME = 'AWS_STORAGE_BUCKET_NAME' AWS_S3_SIGNATURE_NAME = 's3v4', AWS_S3_REGION_NAME = 'AWS_S3_REGION_NAME' AWS_S3_FILE_OVERWRITE = False AWS_DEFAULT_ACL = None AWS_S3_VERITY = True DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
Paste the above configuration into the settings.py file and change the value accordingly. Replace AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY with the access key and secret access key you originally copied or downloaded. You should also change AWS_STORAGE_BUCKET_NAME and AWS_S3_REGION_NAME to the name of the bucket and the S3 bucket.
You can get the domain name by navigating to the S3 bucket and copying the last value in the AWS region column .
Step 6: Check AWS Configuration
The following code sample will upload files directly from the admin panel, but you are free to upload data from other locations.
Here, you have a sample like this:
class Post(models.Model): title = models.CharField(max_length=225, blank=False, null=False) content = models.TextField('Post Body', blank=False, null=False) author = models.CharField(max_length=225, blank=False, null=False) date_published = models.DateTimeField(auto_now=True) image = models.ImageField(upload_to='posts') def __str__(self): return self.title
Make sure you implement the necessary operations like migration, adding it to the admin panel, creating a view window and other things needed for the project. Make sure you follow Django's MVT rules.
Once done, navigate to the admin panel or the created form for file upload and upload an image file.
Navigate to the main page and claim the photo there. If so, right-click the image, then select Open image in new tab . In the new tab containing the images, you'll see an address bar that references the S3 bucket you created from scratch:
Step 7: Collect static files for bucket S3
Add the following configurations to the settings.py file :
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage' AWS_LOCATION = 'static'
Then, open the Command Line Interface (CLI) and run the command:
python manage.py collectstatic --noinput
To confirm everything is working properly, open the S3 bucket in the AWS console. You should see a folder named static .
It's done! Hope the article is useful to you.
You should read it
- How to Install and Configure PostgreSQL in Django
- How to put Django app into maintenance mode
- How to build an authentication system in Django using OAuth
- How to Add Search to a Django App
- How to override default template in django-allauth
- How to Create a CRUD App with Django's Class Based Viewers
- How to Implement ChatGPT in Django
- How to upload photos using Django app
- Using Django API is easy with built-in templates
- Should Django be hosted on PythonAnywhere?
- Model Inheritance là gì trong Django?
- How to create a movie ticket booking system using Django
Maybe you are interested
4 Security Steps to Follow When Using Remote Access Applications
How to Access Android Storage on Windows 11
How to get early access to game demos and releases
Google Essentials Launches: An Easy Solution to Access Google Services on Windows
How to fix wifi error of not being able to access the network and the causes
Summary of 12 ways to quickly access the Settings application on Windows 10