Search code examples
djangodjango-modelsdjango-staticfilesdjango-settings

Django: Best Practice for Storing Images (URLField vs ImageField)


There are cases in a project where I'd like to store images on a model.

For example:

  • Company Logos
  • Profile Pictures
  • Programming Languages
  • Etc.

Recently I've been using AWS S3 for file storage (primarily hosting on Heroku) via ImageField uploads.

I feel like there's a better way to store files than what I've been doing.

For some things (like for the examples above) I think it would make sense to actually just get an image url from a more publically available url than take up space in my own database.

For the experts in the Django community who have built and deployed really professional projects, do you typically store files directly into the Django media folder via ImageField?

or do you normally use a URLField and then pull a url from an API or an image link from the web (e.g., go on any Google image, right click and copy then paste image URL)?

Bonus: What does your image storing setup look like?

Hope this makes sense.

Thanks in advance!


Solution

  • The standard is what you've described, using something like AWS S3 to store the actual image and handle the URL in your database. Here's a few reasons why:

    1. It's cheap. like really cheap
    2. Instead of making your web server serve the files, you're offloading that onto the client (e.g. their browser grabbing the file from S3)
    3. If you're using an ephemeral system (like Heroku), your only option is to use something like S3.
    4. Control. Sure, you can pull an image link from somewhere else that isn't managed by you. But this does not scale. What happens if that server goes offline? What if they take that image down? This way, you control what happens to the objects.

    An example of a decently large internet company but not large enough to run their own infrastructure (like Facebook/Instagram, Google, etc.) is VSCO. They're taking a decent amount of photo uploads every day and they're handling them with AWS.