When hosting a Django application on a cloud hosting provider, such as Heroku, using Amazon S3 for storing static and media files is often recommended. This is because Heroku's and other provider's filesystems are often not intended for permanent storage and does not support file uploads, and any files uploaded to the filesystem are not preserved.
This is because these files can take up a significant amount of disk space, which can increase server costs and slow down the performance of the application.
Using a cloud storage service also has other benefits, such as easy scalability and accessibility, reliability, and cost-effectiveness. Additionally, using a cloud storage service like Amazon S3 allows for better separation of concerns between the web server and the storage of files.
Inside a model containing a file: add this method:
at the bottom of models.py add:
pip install django-storages
pip install boto3
Create an AWS account
Inside .env paste:
AWS_ACCESS_KEY_ID = ' Access Key ID '
AWS_SECRET_ACCESS_KEY=' Secret Access Key '
Add the following configuration