matteok matteok - 1 year ago 144
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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download