Setting MEDIA_URL for the Django Heroku app, Amazon S3

I am trying to configure MEDIA_URL for my Heroku application, which is currently serving static files via STATIC_URL from Amazon S3. Static files work fine, but when I try to add MEDIA_URL in addition to the current STATIC_URL, the pages no longer appear at all and the application stops working.

Current Settings:

AWS_STORAGE_BUCKET_NAME = 'bucketname'
STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
S3_URL = 'http://%s.s3.amazonaws.com/' % AWS_STORAGE_BUCKET_NAME
STATIC_URL = S3_URL
AWS_ACCESS_KEY_ID = 'KEY'
AWS_SECRET_ACCESS_KEY = 'SECRET_KEY'

When I add:

MEDIA_URL = S3_URL
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'

what causes the problem. In particular, MEDIA_URL is problematic, because when DEFAULT_FILE_STORAGE is deleted, it still has the same problem. But I am trying to determine how to best serve the media uploaded by the user using this to no avail.

If someone has an idea of ​​how best to achieve this, it would be very helpful.

+5
2

STATIC_URL MEDIA_URL , .

/ , . , ( ):

django_storages django_compressor, S3, STATIC_URL. settings.py:

COMPRESS_URL = "https://s3.amazonaws.com/bucketname/"
STATIC_URL = COMPRESS_URL

dev MEDIA_URL STATIC_URL. , True env False Heroku, _ env.

:

background-image: url({% if env == 'True' %}{{ MEDIA_URL }}{% else %}{{ STATIC_URL }}{% endif %}{{ course.image }});

, . , - , :)

: - S3.

+2

, .

s3utils.py , settings.py:

from storages.backends.s3boto import S3BotoStorage

StaticRootS3BotoStorage = lambda: S3BotoStorage(location='static')
MediaRootS3BotoStorage  = lambda: S3BotoStorage(location='media')

settings.py:

DEFAULT_FILE_STORAGE = 'myproyect.s3utils.MediaRootS3BotoStorage'
STATICFILES_STORAGE = 'myproyect.s3utils.StaticRootS3BotoStorage'
+4

All Articles