Serving uploaded files from multiple storages

32 views
Skip to first unread message

Hugo Osvaldo Barrera

unread,
Aug 11, 2015, 11:18:17 PM8/11/15
to django...@googlegroups.com
I've an app where files are uploaded to differente storages:

Certain files uploaded via de the admin are uploaded to S3 (and served
via a CDN) (using a storage from django-storages).
Other files (user-uploaded files), are saved into into our local storage
(using the default FileField storage). These are served by nginx, since
they're usually only served between 0 and 2 times.

In other words, I have:

image = ImageField(
upload_to='media/', height_field='height', width_field='width'
)

And:

catalog_photo = ImageField(
storage=S3BotoStorage(),
)

I also need to show these files in different templates (and need the
admin to show the properly too!).
If I set MEDIA_URL to point to where I upload the first group, them URLs
for the second are broken, and viceversa.

How can I deal with this? Does the field have anything I can set that I
missed?
Also, how can I avoid this breaking in the admin, where editing the
template would be quite a bit more effort (that I'd like to avoid)?

Thanks,

--
Hugo Osvaldo Barrera

Javier Guerra Giraldez

unread,
Aug 12, 2015, 2:11:02 AM8/12/15
to django...@googlegroups.com
On Tue, Aug 11, 2015 at 6:17 PM, 'Hugo Osvaldo Barrera' via Django
users <django...@googlegroups.com> wrote:
> I've an app where files are uploaded to differente storages:


i would create a new storage object with a configuration that
associates some prefix with other 'backend' storage objects. then
implement each storage method by stripping the prefix, using it to
determine the apropriate backend, and calling it with the rest of the
path.

--
Javier

Hugo Osvaldo Barrera

unread,
Aug 12, 2015, 2:41:00 PM8/12/15
to django...@googlegroups.com
So, if I understand correctly, have an intermediate storage that
delegates to the proper one according to a path prefix received, and
expose the paths as full URLs to include in my template, is that it?

Makes sense. A wee bit ugly for my taste, but I guess there's no cleaner
solution.
Reply all
Reply to author
Forward
0 new messages