自定义django存储s3 url?
我使用django storages让用户将图像文件上传到我的S3,我有一个cloudfront发行版可以在S3中使用bucket 我可以将文件上载到s3,但我无法将图像文件的url更改为使用cloudfront分发url url始终设置为s3 bucket url 有没有办法自定义url自定义django存储s3 url?,django,amazon-s3,cdn,amazon-cloudfront,django-storage,Django,Amazon S3,Cdn,Amazon Cloudfront,Django Storage,我使用django storages让用户将图像文件上传到我的S3,我有一个cloudfront发行版可以在S3中使用bucket 我可以将文件上载到s3,但我无法将图像文件的url更改为使用cloudfront分发url url始终设置为s3 bucket url 有没有办法自定义url 谢谢我采取的解决这个问题的方法是创建一个名为“static_cdn”的新模板标签;它会做一些检查,说“我是本地的、开发的、生产的等等”,并且会根据域名所在的位置适当地搜索域名。如果我只是在本地闲逛(或者,至少
谢谢我采取的解决这个问题的方法是创建一个名为“static_cdn”的新模板标签;它会做一些检查,说“我是本地的、开发的、生产的等等”,并且会根据域名所在的位置适当地搜索域名。如果我只是在本地闲逛(或者,至少,我还没有到那里),那么产生CDN流量是没有意义的 我考虑的另一种方法是完全覆盖默认的静态标记,并将逻辑放在其中,但现在我想保持粒度,以便出于某种原因,如果我想直接从S3而不是生产服务器上的CloudFront获取,我就有这种能力 编辑:示例代码可能如下所示:
# Import: Django
from django.template import Library
from django.templatetags.static import static
# Static CDN
# - We could probably go in and overwrite the default static template tag
# and decide to use the CDN or not; for now, though, I want the option
# (even remotely) to explicitly use the CDN or not.
def static_cdn(url):
if not LOCAL_ENVIRONMENT:
# StackOverflow: Do not do this! Import/Write/etc an urljoin (see Django code
# for examples - you can probably import the version Django uses internally.
return 'https://{0}{1}{2}'.format(AWS_CLOUDFRONT_DOMAIN, STATIC_URL, url)
else:
return static(url)
# Register
register = Library()
register.simple_tag(static_cdn)
在产品设置中,请使用:
AWS\u S3\u CUSTOM\u DOMAIN='cdn.mydomain.com'
源文档:也许您希望覆盖