【问题标题】:Create custom domain CNAME for AWS S3 static and media files in Django在 Django 中为 AWS S3 静态和媒体文件创建自定义域 CNAME
【发布时间】:2020-04-22 21:27:50
【问题描述】:

首先想说我不想使用 Cloudfront。

假设网站是example.com
存储桶名称 S3:example
静态/媒体文件的 URL:example.s3.amazonaws.com

通过 CNAME 服务静态和媒体文件需要执行哪些步骤,如下所示: media.example.com

在域的 DNS 中,CNAME media 我应该准确地指向什么?

现在图片的投放方式如下:https://example.s3.amazonaws.com/static/image.jpg
想要这样:https://media.example.com/static/image.jpg

还有一个原因是我可以在站长工具中验证CNAME。

生产.py

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/
# STATIC FILE CONFIGURATION
# ------------------------------------------------------------------------------
# See: https://docs.djangoproject.com/en/dev/ref/settings/#static-root
STATIC_ROOT = str(ROOT_DIR('staticfiles'))

# See: https://docs.djangoproject.com/en/dev/ref/settings/#static-url
STATIC_URL = '/static/'

# See: https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#std:setting-STATICFILES_DIRS
STATICFILES_DIRS = (
    str(APPS_DIR.path('static')),
)

# See: https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#staticfiles-finders
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'compressor.finders.CompressorFinder',
)

# MEDIA CONFIGURATION
# ------------------------------------------------------------------------------
# See: https://docs.djangoproject.com/en/dev/ref/settings/#media-root
MEDIA_ROOT = str(APPS_DIR('media'))

# See: https://docs.djangoproject.com/en/dev/ref/settings/#media-url
MEDIA_URL = '/media/'

# DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
DEFAULT_FILE_STORAGE = 'config.custom_storages.MediaStorage'
THUMBNAIL_DEFAULT_STORAGE = 'config.custom_storages.MediaStorage'


AWS_ACCESS_KEY_ID = env('DJANGO_AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = env('DJANGO_AWS_SECRET_ACCESS_KEY')
AWS_STORAGE_BUCKET_NAME = env('DJANGO_AWS_STORAGE_BUCKET_NAME')
AWS_S3_REGION_NAME = env('REGION_NAME')  # e.g. us-east-2
AWS_S3_CUSTOM_DOMAIN = '{}.s3.amazonaws.com'.format(AWS_STORAGE_BUCKET_NAME)
AWS_AUTO_CREATE_BUCKET = True
AWS_QUERYSTRING_AUTH = False
AWS_S3_CALLING_FORMAT = OrdinaryCallingFormat()
AWS_DEFAULT_ACL = "public-read"

# AWS cache settings, don't change unless you know what you're doing:
AWS_EXPIRY = 60 * 60 * 24 * 7

# TODO See: https://github.com/jschneier/django-storages/issues/47
# Revert the following and use str after the above-mentioned bug is fixed in
# either django-storage-redux or boto
AWS_HEADERS = {
    'Cache-Control': six.b('max-age=%d, s-maxage=%d, must-revalidate' % (
        AWS_EXPIRY, AWS_EXPIRY))
}

# URL that handles the media served from MEDIA_ROOT, used for managing
# stored files.
MEDIAFILES_LOCATION = 'media'
MEDIA_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, MEDIAFILES_LOCATION)


# Static Assests
# ------------------------
STATICFILES_STORAGE = 'config.custom_storages.CachedS3BotoStaticStorage'
COMPRESS_STORAGE = 'config.custom_storages.CachedS3BotoStaticStorage'

AWS_S3_SECURE_URLS = True

STATICFILES_LOCATION = 'static'
STATIC_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, STATICFILES_LOCATION)

【问题讨论】:

  • S3 不提供自定义域和 HTTPS,而 CloudFront 是规范的解决方案。你为什么不想使用它?
  • 嗯,这很奇怪,我有几个 CNAMES 指向几个存储桶,并且有静态站点,它们像 assets.example.com 一样工作并加载站点 - 但在这种情况下,我确实想要将它用作 django 的静态/媒体存储 - 所以它应该可以工作 - 你说规范在 S3 上不起作用?
  • S3 不支持 both HTTPS 和 自定义域。您可以拥有 HTTPS(带有 amazonaws.com URL),也可以拥有自定义域(仅限 HTTP),不能同时拥有。但如果您认为单独使用 S3 比 S3 + CloudFront 便宜得多,我认为您也可能误解了定价方案。对于通过 CDN 提取的内容,S3 不收取带宽费用,无论有多少边缘站点对其进行缓存,CloudFront 也不收取存储费用,后者在许多领域的带宽费用也比 S3 略低(例如 0.085 美元/GB CF in美国与 0.090 美元/GB S3 在 us-east-1)。
  • @Michael-sqlbot 所说的。在基本级别上,您不能在 S3 上使用自定义域使用 SSL,因为 S3 不是服务器,它是对象存储,并且要创建安全连接,您的域的证书需要安装在服务器上,即S3 本身不支持,相反,Amazon 提供 CloudFront 作为解决方案。如果您不想利用 CloudFront 的缓存功能,只需将最大 TTL 设置为 0;对象不会被缓存。
  • 从 How do I use CloudFront to serve HTTPS requests for my Amazon S3 bucket? 开始——它解释了如何使用 CloudFront 为 S3 存储桶设置自定义域 + HTTPS——如果您遇到任何问题,请告诉我们。

标签: django amazon-s3 cdn cname django-staticfiles


【解决方案1】:

如果它对某人有帮助 - 最初我不打算使用 Cloudfront - 但由于使用了 HTTPS,我不得不这样做。 2 个很好的帮助文档,说明如何使用 @Michael-sqlbot 和 @hephalump 帮助进行设置。

  1. https://aws.amazon.com/premiumsupport/knowledge-center/cloudfront-https-requests-s3/

  2. https://medium.com/@brodartec/hosting-a-static-site-with-https-enabled-using-aws-s3-cloudfront-and-godaddy-826dae41fdc6

【讨论】:

    猜你喜欢
    • 2017-11-25
    • 2020-11-15
    • 2012-10-29
    • 2019-08-27
    • 2017-05-02
    • 2021-06-19
    • 2018-07-07
    • 1970-01-01
    • 2020-05-23
    相关资源
    最近更新 更多