我正在使用django-storages让用户将图像文件上传到我的s3,并且我在S3中有一个cloudfront发行版可以与存储桶一起工作。
我可以将文件上传到s3,但我不能将图像文件的url更改为使用cloudfront分发url。
url始终设置为s3存储桶url。
有没有办法自定义url?
谢谢
发布于 2018-10-12 05:02:10
在productions设置中使用:
AWS_S3_CUSTOM_DOMAIN = 'cdn.mydomain.com'
来源文档:https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html
发布于 2013-10-09 01:29:23
我采用的一种解决这个问题的方法是创建一个名为‘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)https://stackoverflow.com/questions/18938760
复制相似问题