matteok matteok - 7 months ago 78
Python Question

Django ImageField url slow when using Amazon s3

In my Django Application currently running on Heroku I noticed that retrieving url's from ImageFields takes forever when hosting on s3.
The application is written using Django Rest Framework. When I try to retrieve a list of objects that have image fields the response is so slow Heroku throws a timeout error. I assume this is because boto has to retrieve a url from s3 for each individual ImageField upon each request.
How can I speed up the process to prevent these timeout errors?


You can create a new field in your models, for example, image_url.

class YourModel(...):
    image_url = models.CharField(...)
    # other fields

When the image is uploaded/saved the first time, retrieve its URL and populate image_url field with this value. You'll need to save your model again, though.

You can use this value later when required.


This may lead to unnecessary database lookups. If, however, you use Memcached or something like that to cache database, I wouldn't worry.