首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义django-storage s3 url?

自定义django-storage s3 url?
EN

Stack Overflow用户
提问于 2013-09-22 07:33:22
回答 2查看 1.4K关注 0票数 2

我正在使用django-storages让用户将图像文件上传到我的s3,并且我在S3中有一个cloudfront发行版可以与存储桶一起工作。

我可以将文件上传到s3,但我不能将图像文件的url更改为使用cloudfront分发url。

url始终设置为s3存储桶url。

有没有办法自定义url?

谢谢

EN

回答 2

Stack Overflow用户

发布于 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

票数 2
EN

Stack Overflow用户

发布于 2013-10-09 01:29:23

我采用的一种解决这个问题的方法是创建一个名为‘static_cdn’的新模板标签;它会做一些检查,以确定“我是本地的,开发的,生产的,等等”,并根据域名的位置适当地转换域名。如果我只是在本地胡闹(或者,至少我还没有到那里),那么生成CDN流量是没有意义的。

我考虑的另一种方法是完全覆盖默认的静态标记,并将逻辑放在其中,但现在我希望保持粒度,这样如果出于某种原因,我希望直接从S3而不是生产服务器上的CloudFront获取数据,我就有这种能力。

编辑:示例代码可能如下所示:

代码语言:javascript
复制
# 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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18938760

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档