Django This backend doesn't support absolute paths after integrating aws S3 bucket

I am facing following problems after add image compressing code in my models.py :

problem 1: Image resizing not working after integrating aws S3 bucket. problem 2: getting this error page after image upload NotImplementedError at /blog/edit/hello/ This backend doesn't support absolute paths. here error details of my terminal

"C:\Users\Fdg\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\files\storage.py", line 123, in path
    raise NotImplementedError("This backend doesn't support absolute paths.")
NotImplementedError: This backend doesn't support absolute paths.
[27/Dec/2021 23:49:44] "POST /blog/edit/hello/ HTTP/1.1" 500 118419

my settings.py

AWS_ACCESS_KEY_ID  = "my acess key"
AWS_SECRET_ACCESS_KEY = "my secret key"
AWS_STORAGE_BUCKET_NAME = "my bucket name"
AWS_S3_CUSTOM_DOMAIN = 'my aws domain name' 
AWS_S3_OBJECT_PARAMETERS = {'CacheControl': 'max-age=86400'}
AWS_DEFAULT_ACL = 'public-read'
AWS_S3_FILE_OVERWRITE = False
AWS_QUERYSTRING_AUTH = False
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'


AWS_MEDIA = '/media/'
AWS_STATIC = 'static'
STATIC_URL = 'https://%s/%s/' % (AWS_S3_CUSTOM_DOMAIN, AWS_STATIC)
MEDIA_URL = 'https://%s/%s/' % (AWS_S3_CUSTOM_DOMAIN, AWS_MEDIA)
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

here is my models.py for upload images:

blog_cover_image = models.ImageField(upload_to='blog/images/',validators=[validate_file_size,FileExtensionValidator( ['png','jpg'] )],blank=True,null=True) 
#image comressing start 
if self.blog_cover_image:    
            img = Image.open(self.blog_cover_image.path)  
            
img.save(self.blog_cover_image.path,quality=20,optimize=True)
#image compression end

Though I am getting this error but image is uploading on aws S3 bucket. Why I am getting this error and how to overcome it?

updating questing: this error happening only for this image compressing code:

#image comressing start 
    if self.blog_cover_image:    
                img = Image.open(self.blog_cover_image.path)  
                
    img.save(self.blog_cover_image.path,quality=20,optimize=True)
    #image compression end 

how to use image compression for aws s3 bucket?

1 Like

Hi,

I recall facing this error in my code a year or two ago. I no longer use the code that was causing me the headache, but some googling lead me to this post which seems to trigger a memory which makes me think this might be your solution.

2 Likes