Unable to upload images in Django Project on AWS S3, despite configuring ImageField and storages
I’m working on a Django project, and I’ve successfully connected it to AWS S3 for storing media files. However, I’m unable to upload images via the Django admin panel. Text-only articles are uploaded fine, but as soon as I try to upload an image, it causes issues and the upload fails. I've configured the ImageField in my model and set up AWS S3 storage, but the image uploads still don't work.
class Article(models.Model):
CATEGORY_CHOICES = [
('TECH', 'Technology'),
('PHI', 'Philosophy'),
('SCI', 'Science'),
('CUL', 'Culture'),
('BUS', 'Business'),
]
title = models.CharField(max_length=200)
content = models.TextField()
author = models.CharField(max_length=100)
published_date = models.DateTimeField(default=now)
is_published = models.BooleanField(default=False) # Control publishing status
image = models.ImageField(upload_to='articles/', blank=True, null=True)
is_featured = models.BooleanField(default=False)
category = models.CharField(default= False, max_length=10, choices=CATEGORY_CHOICES)
def __str__(self):
return self.title
AWS S3 Bucket permissions: I’ve ensured that my AWS S3 bucket is set to allow public access, and the necessary permissions for writing objects are in place.
STATIC_URL = f"https://{AWS_STORAGE_BUCKET_NAME}.s3.{AWS_S3_REGION_NAME}.amazonaws.com/static/"
MEDIA_URL = f"https://{AWS_STORAGE_BUCKET_NAME}.s3.{AWS_S3_REGION_NAME}.amazonaws.com/media/"
MEDIA_URL = f"https://{AWS_STORAGE_BUCKET_NAME}.s3.{AWS_S3_REGION_NAME}.amazonaws.com/media/"
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': getenv('PGDATABASE'),
'USER': getenv('PGUSER'),
'PASSWORD': getenv('PGPASSWORD'),
'HOST': getenv('PGHOST'),
'PORT': getenv('PGPORT', 5432),
'DISABLE_SERVER_SIDE_CURSORS': True,
}
}
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
Troubleshooting:
I’ve confirmed that the Django application has internet access to AWS S3.
I’ve verified that the S3 bucket is properly configured with the correct region and permissions.
I’ve also set the FILE_UPLOAD_MAX_MEMORY_SIZE in settings.py to 10MB.
I tried re-uploading images with different file sizes, but the issue persists.
Despite all of these steps, the issue still exists where images fail to upload, but text-only content works fine.
The error log is the following:
File "/opt/venv/lib/python3.13/site-packages/django/utils/deprecation.py", line 129, in __call__
response = response or self.get_response(request)
~~~~~~~~~~~~~~~~~^^^^^^^^^
File "/opt/venv/lib/python3.13/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "/opt/venv/lib/python3.13/site-packages/django/utils/deprecation.py", line 129, in __call__
response = response or self.get_response(request)
~~~~~~~~~~~~~~~~~^^^^^^^^^
~~~~~~~~~~~~~~~~~^^^^^^^^^
File "/opt/venv/lib/python3.13/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "/opt/venv/lib/python3.13/site-packages/django/utils/deprecation.py", line 129, in __call__
response = response or self.get_response(request)
~~~~~~~~~~~~~~~~~^^^^^^^^^
File "/opt/venv/lib/python3.13/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "/opt/venv/lib/python3.13/site-packages/whitenoise/middleware.py", line 123, in __call__
return self.get_response(request)
~~~~~~~~~~~~~~~~~^^^^^^^^^
File "/opt/venv/lib/python3.13/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "/opt/venv/lib/python3.13/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = middleware_method(
Additional Information:
Database: I recently migrated my Django project from a local PostgreSQL database to NeonDB for cloud hosting. This migration seems unrelated to the image upload issue but could potentially be worth mentioning in case there’s an indirect effect.
AWS S3 Region: eu-north-1
Storage Backend: django-storages