【问题标题】:Django compressor and CloudfrontDjango 压缩器和 Cloudfront
【发布时间】:2020-09-12 04:05:28
【问题描述】:

我使用 AWS S3 来存储我的 Django 静态文件(使用 django-storages)。以及备用云端域名来提供这些文件。

我无法通过python manage.py compress 完成 Django 压缩器。

这是我看到的错误:

CommandError: An error occurred during rendering /home/ubuntu/foldername/templates/home/xxx.html: 'https://xxx.domainname.com/static/plugins/xxx-xxx/xxx.js' isn't accessible via COMPRESS_URL ('https://s3bucket-static.s3.amazonaws.com/') and can't be compressed

所以我尝试使用COMPRESS_URL 的默认云端地址、备用域名和 S3 存储桶:

只有https://domainname.com/static/ 没有错误。发生的情况是,compress 完成后,CACHE 文件夹出现在本地实例 staticfiles 文件夹中。这是错误的,因为我希望 CACHE 文件夹位于 S3 存储桶内。

这是我对 django 压缩器的设置:

# django-compressor
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'compressor.finders.CompressorFinder',
)

COMPRESS_ENABLED = True
COMPRESS_OFFLINE = True

if not DEBUG:
    COMPRESS_URL = env('COMPRESS_URL')

这是我的 django-storages 设置:

if not DEBUG:
    DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
    STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

    AWS_ACCESS_KEY_ID = env('AWS_ACCESS_KEY_ID')
    AWS_SECRET_ACCESS_KEY = env('AWS_SECRET_ACCESS_KEY')
    AWS_STORAGE_BUCKET_NAME = env('AWS_STORAGE_BUCKET_NAME')

    AWS_DEFAULT_ACL = None
    AWS_CLOUDFRONT_DOMAIN_NAME = env('AWS_CLOUDFRONT_DOMAIN_NAME')
    AWS_S3_CUSTOM_DOMAIN = f'{AWS_CLOUDFRONT_DOMAIN_NAME}.com'

    AWS_S3_OBJECT_PARAMETERS = {
        'CacheControl': 'max-age=86400',
    }
    AWS_LOCATION = 'static'

静态文件设置:

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles/')
STATICFILES_DIRS = os.path.join(BASE_DIR, 'static/')

【问题讨论】:

    标签: django amazon-web-services amazon-s3 django-storage django-compressor


    【解决方案1】:

    在我将 django-compressor 设置更改为此之后,它起作用了:

    # django-compressor
    COMPRESS_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
    
    STATICFILES_FINDERS = (
        'django.contrib.staticfiles.finders.FileSystemFinder',
        'django.contrib.staticfiles.finders.AppDirectoriesFinder',
        'compressor.finders.CompressorFinder',
    )
    
    COMPRESS_ENABLED = True
    COMPRESS_OFFLINE = True
    
    if not DEBUG:
        STATIC_URL = f'https://{AWS_S3_CUSTOM_DOMAIN}/static/'
        COMPRESS_ROOT = STATIC_ROOT
        COMPRESS_URL = STATIC_URL
    

    【讨论】:

      猜你喜欢
      • 2018-06-25
      • 2017-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-21
      • 2014-10-24
      • 1970-01-01
      • 2018-04-19
      相关资源
      最近更新 更多