为Django Heroku App,Amazon S3设置MEDIA_URL

时间:2020-12-19 23:03:08

I've been attempting to set up a MEDIA_URL for my Heroku app, which is currently serving static files via STATIC_URL from Amazon S3. The static files are working fine, but when I attempt to add a MEDIA_URL in addition to the current STATIC_URL, the pages no longer render at all and the app ceases to work.

我一直在尝试为我的Heroku应用程序设置MEDIA_URL,该应用程序目前通过Amazon S3的STATIC_URL提供静态文件。静态文件工作正常,但当我尝试添加除当前STATIC_URL之外的MEDIA_URL时,页面根本不再呈现,并且应用程序停止工作。

The current settings are:

目前的设置是:

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'

that causes the issue. Specifically, the MEDIA_URL is problematic as when DEFAULT_FILE_STORAGE is deleted, it still has the same issue. But I'm trying to determine best how to serve user uploaded media through this unsuccessfully.

导致问题。具体来说,MEDIA_URL存在问题,因为删除DEFAULT_FILE_STORAGE时,它仍然存在同样的问题。但我正试图通过此次失败来确定如何为用户上传的媒体提供最佳服务。

If anyone has any insight how best to achieve this, it would be most appreciated.

如果有人有任何见解如何最好地实现这一点,那将是非常感激的。

2 个解决方案

#1


2  

STATIC_URL and MEDIA_URL can't have the same value, it raises an error.

STATIC_URL和MEDIA_URL不能具有相同的值,它会引发错误。

I had/still have the same problem and couldn't find a clean way to do it. Wait for a better answer, but this is how I solved it (ugly hack):

我有/仍然有同样的问题,找不到干净的方法来做到这一点。等待更好的答案,但这就是我解决它的方式(丑陋的黑客):

I'm using django_storages and django_compressor. Since the media files appear in the root of my S3 bucket I can access them through STATIC_URL. In my settings.py:

我正在使用django_storages和django_compressor。由于媒体文件出现在我的S3存储桶的根目录中,因此我可以通过STATIC_URL访问它们。在我的settings.py中:

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

In my local dev environment I use MEDIA_URL and for production STATIC_URL. Set an environmental variable that has the value True for your local env and False on Heroku and put it in a context_processor env.

在我的本地开发环境中,我使用MEDIA_URL和生产STATIC_URL。为本地env设置一个值为True的环境变量,在Heroku上设置为False,并将其放在context_processor env中。

Then you can access the media files in your templates like this:

然后,您可以访问模板中的媒体文件,如下所示:

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

It works, but it is ugly. Hopefully somebody found a better solution so I ran refactor my code :)

它有效,但很难看。希望有人找到更好的解决方案,所以我运行重构我的代码:)

edit There's a better way: this app lets you create a static and a media folder in S3.

编辑有一种更好的方法:这个应用程序允许您在S3中创建静态和媒体文件夹。

#2


4  

This solution works quite well, as described below.

该解决方案非常有效,如下所述。

Create a file called s3utils.py in the same directory as settings.py:

在与settings.py相同的目录中创建名为s3utils.py的文件:

from storages.backends.s3boto import S3BotoStorage

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

Then in settings.py:

然后在settings.py中:

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

#1


2  

STATIC_URL and MEDIA_URL can't have the same value, it raises an error.

STATIC_URL和MEDIA_URL不能具有相同的值,它会引发错误。

I had/still have the same problem and couldn't find a clean way to do it. Wait for a better answer, but this is how I solved it (ugly hack):

我有/仍然有同样的问题,找不到干净的方法来做到这一点。等待更好的答案,但这就是我解决它的方式(丑陋的黑客):

I'm using django_storages and django_compressor. Since the media files appear in the root of my S3 bucket I can access them through STATIC_URL. In my settings.py:

我正在使用django_storages和django_compressor。由于媒体文件出现在我的S3存储桶的根目录中,因此我可以通过STATIC_URL访问它们。在我的settings.py中:

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

In my local dev environment I use MEDIA_URL and for production STATIC_URL. Set an environmental variable that has the value True for your local env and False on Heroku and put it in a context_processor env.

在我的本地开发环境中,我使用MEDIA_URL和生产STATIC_URL。为本地env设置一个值为True的环境变量,在Heroku上设置为False,并将其放在context_processor env中。

Then you can access the media files in your templates like this:

然后,您可以访问模板中的媒体文件,如下所示:

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

It works, but it is ugly. Hopefully somebody found a better solution so I ran refactor my code :)

它有效,但很难看。希望有人找到更好的解决方案,所以我运行重构我的代码:)

edit There's a better way: this app lets you create a static and a media folder in S3.

编辑有一种更好的方法:这个应用程序允许您在S3中创建静态和媒体文件夹。

#2


4  

This solution works quite well, as described below.

该解决方案非常有效,如下所述。

Create a file called s3utils.py in the same directory as settings.py:

在与settings.py相同的目录中创建名为s3utils.py的文件:

from storages.backends.s3boto import S3BotoStorage

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

Then in settings.py:

然后在settings.py中:

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