0

I have a load of models that have TextFields which (sometimes) contain links to images which the users have uploaded when they edited those fields.

So a field might contain something like this:-

This is my image

<img src="/media/uploads/myimage.jpg">

This works fine at the moment, as all of my media is being served from a directory on my web server by nginx.

However, I want to move all my user-uploaded media to Amazon S3 and not serve media from my server.

I've got everything working for the models that have images in ImageFields, as they just work out the path for the image using the django-storages API. But the images that are stored as above inside TextFields won't work any more, as they are relative paths, not S3 paths.

Is there a way to make this work without having to edit all the contents of all of the TextFields to change the img path to be the full S3 path? Not only does that seem like a very laborious process (even if I can do it programmatically) but what if I subsequently want to change to another storage service? I'll have to do it again!

It would be better if an image tag with a relative URL like that just knew that it had to find the real path using the full S3 URL but I'm not sure how to do that.

bodger
  • 1,112
  • 6
  • 24
  • That's a complicated question. What if some of the images should be served from local, and other ones from S3? You could use a regex or something to rewrite all relative paths to absolute ones before serving a response, but the solution is not ideal. If this were a template, you should have used {% static %} tag to resolve absolute URL - this would be the best solution. – Alexandr Tatarinov Jul 08 '20 at 17:11
  • These are user-uploaded media files, not static files. So the static tag won't help. – bodger Jul 08 '20 at 17:40

0 Answers0