[英]Django Storages using s3boto ignoring MEDIA_URL
I am trying to use django-storages with s3boto in my app and trying to serve media and static files from s3. 我试图在我的应用程序中使用django-storage与s3boto并尝试从s3提供媒体和静态文件。
I have the following settings in my settings file: 我的设置文件中有以下设置:
AWS_STORAGE_BUCKET_NAME = '<bucket_name>'
AWS_S3_ACCESS_KEY_ID = '<access_key>'
AWS_S3_SECRET_ACCESS_KEY = '<secret>'
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
STATICFILES_LOCATION = 'static'
STATICFILES_STORAGE = '<custom_storage_satic>'
MEDIAFILES_LOCATION = 'media'
DEFAULT_FILE_STORAGE = '<custom_storage_media>'
And my custom_storages.py is 我的custom_storages.py是
from django.conf import settings
from storages.backends.s3boto import S3BotoStorage
class StaticStorage(S3BotoStorage):
location = settings.STATICFILES_LOCATION
class MediaStorage(S3BotoStorage):
location = settings.MEDIAFILES_LOCATION
When I create an image in django, instead of getting the relative path to my image starting with 当我在django中创建图像时,而不是从我的图像开始的相对路径
image.url
'/media/image/<rest_of_the_path>.jpg'
I am getting the absolute url, which is something like 我得到了绝对的网址,就像这样
image.url
'https://<s3_bucket_name>.s3.amazonaws.com/media/image/original/'
When I use local storage instead of s3boto, it works as expected and gives me the relative path. 当我使用本地存储而不是s3boto时,它按预期工作并给我相对路径。 Am I missing something here?
我在这里错过了什么吗?
I struck the same issue when attempting to use the Imgix CDN for my S3 media (I suspect we're both using the same tutorial based on your use of the custom_storages.py
override). 当我尝试将Imgix CDN用于我的S3媒体时,我遇到了同样的问题(我怀疑我们都使用了基于你使用
custom_storages.py
覆盖的相同教程 )。
Here is an abridged version of the S3BotoStorage class in the django-storages framework. 这是django- storages框架中的S3BotoStorage类的删节版本。 This excerpt highlights the important properties and methods for this issue, namely the
custom-domain
property. 此摘录突出显示了此问题的重要属性和方法,即
custom-domain
属性。
class S3BotoStorage(Storage):
location = setting('AWS_LOCATION', '')
custom_domain = setting('AWS_S3_CUSTOM_DOMAIN')
def url(self, name, headers=None, response_headers=None, expire=None):
# Preserve the trailing slash after normalizing the path.
name = self._normalize_name(self._clean_name(name))
if self.custom_domain:
return "%s//%s/%s" % (self.url_protocol, self.custom_domain, filepath_to_uri(name))
As you can see in the url
method, a URL is generated to override the STATIC_URL
and MEDIA_URL
Django settings. 正如您在
url
方法中看到的,生成了一个URL来覆盖STATIC_URL
和MEDIA_URL
Django设置。 Currently the domain of the URL is created with the AWS_S3_CUSTOM_DOMAIN
setting, which is why you continue to see the static S3 URL for media files. 目前,URL的域是使用
AWS_S3_CUSTOM_DOMAIN
设置创建的,这就是您继续查看媒体文件的静态S3 URL的原因。
So first, in your Django settings file, add a setting describing your CDN's domain. 首先,在您的Django设置文件中,添加一个描述您的CDN域的设置。
IMGIX_DOMAIN = 'example.imgix.net'
Then, similar to the override of the location
property, add an override to the custom_domain
property in your MediaStorage
class. 然后,类似于
location
属性的覆盖,向MediaStorage
类中的custom_domain
属性添加覆盖。
class MediaStorage(S3BotoStorage):
location = settings.MEDIAFILES_LOCATION
custom_domain = settings.IMGIX_DOMAIN
Now the final URL to your media files should begin with your CDN's domain, followed by the relative path to your file on the S3 bucket. 现在,媒体文件的最终URL应该从您的CDN域开始,然后是S3存储桶上文件的相对路径。
如果从S3存储桶提供静态媒体,则必须使用绝对URL,因为媒体是从完全不同的服务器提供的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.