简体   繁体   English

Django 图像压缩不适用于 aws s3 存储桶

[英]Django image compression not working for aws s3 bucket

I trying to compressing image before upload on aws s3 bucket.我试图在上传到 aws s3 存储桶之前压缩图像。 But I am getting this error This backend doesn't support absolute paths after add this image compressing code:但是我得到这个错误This backend doesn't support absolute paths

#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 

problem 1: Image resizing not working after integrating aws S3 bucket.问题 1:集成 aws S3 存储桶后图像大小调整不起作用。 problem 2: getting this error page after image upload NotImplementedError at /blog/edit/hello/ This backend doesn't support absolute paths.问题 2:图片上传后出现此错误页面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

here is my models.py for upload images:这是我用于上传图片的models.py

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.虽然我收到此错误,但图像正在 aws S3 存储桶上上传。 Why I am getting this error and how to overcome it?为什么我会收到此错误以及如何克服它? How to use image compressing in Django for aws s3 bucket?如何在 Django 中为 aws s3 存储桶使用图像压缩?

replace the path to url将路径替换为 url

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.url)  
        
        img.save(self.blog_cover_image.url,quality=20,optimize=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM