我已经设置了一个AWS S3存储桶,以便使用应用程序django-storage在远程CDN中传输我的静态文件,一切正常,直到我尝试压缩我的静态文件,然后使用django_compressor上传到S3.
我根据django-storages的django_compressor文档设置了所有变量(https://django_compressor.readthedocs.org/en/latest/remote-storages/index.html)
我使用'manage.py collectstatic'上传了S3中的所有文件,然后:
当我'manage.py compress'时,我收到此错误:
CommandError: An error occured during rendering ../templates/base.html: 'https://my_bucket.s3.amazonaws.com/css/bootstrap.2.3.1.css' isn't accessible via COMPRESS_URL ('https://my_bucket.s3-external-3.amazonaws.com/') and can't be compressed
Run Code Online (Sandbox Code Playgroud)
我的设置有什么问题?
这是我的django-storages和django_compressor的settings.py配置:
COMPRESS_URL = 'https://mybucket_name.s3-external-3.amazonaws.com/'
STATIC_URL = COMPRESS_URL
DEFAULT_FILE_STORAGE = 'my_project.boto_custom.CachedS3BotoStorage'
AWS_ACCESS_KEY_ID = 'XXX'
AWS_SECRET_ACCESS_KEY = 'XXX'
AWS_STORAGE_BUCKET_NAME = 'mybucket_name'
COMPRESS_ROOT = STATIC_ROOT
COMPRESS_STORAGE = 'my_project.boto_custom.CachedS3BotoStorage'
STATICFILES_STORAGE = 'my_project.boto_custom.CachedS3BotoStorage'
COMPRESS_OFFLINE = True
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助
我正在使用django-compressor和django-storage来在S3上提供我的压缩文件(使用这些说明:http://django_compressor.readthedocs.org/en/latest/remote-storages/#using-staticfiles).它在运行"compress"管理命令后最初工作得很好,但大约一小时后,压缩的css和js文件返回403 Forbidden错误,即使我没有对文件进行任何更改.我似乎无法隔离问题,所以任何帮助将不胜感激.
以下是我使用的设置:
COMPRESS_ENABLED = True
COMPRESS_URL = "http://mybucket.s3.amazonaws.com/"
COMPRESS_STORAGE = 'sm.storage.CachedS3BotoStorage'
COMPRESS_YUI_BINARY = os.path.join(PROJECT_ROOT, 'jars/yuicompressor-2.4.7.jar')
COMPRESS_CSS_FILTERS = ['compressor.filters.yui.YUICSSFilter',
'compressor.filters.css_default.CssAbsoluteFilter']
COMPRESS_JS_FILTERS = ['compressor.filters.yui.YUIJSFilter',]
COMPRESS_OFFLINE = True
STATICFILES_STORAGE = COMPRESS_STORAGE
STATIC_URL = COMPRESS_URL
STATIC_ROOT = '/path/to/static/'
STATICFILES_DIRS = (
os.path.join(PROJECT_ROOT, 'static'),
)
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage' # I'm using this for uploaded media
AWS_ACCESS_KEY_ID = 'myaccesskey'
AWS_SECRET_ACCESS_KEY = 'mysecretkey'
AWS_STORAGE_BUCKET_NAME = 'mybucket'
AWS_S3_FILE_OVERWRITE = True
AWS_HEADERS = {
'Cache-Control': 'public, max-age=31536000', #(1 year)
}
Run Code Online (Sandbox Code Playgroud)
更新:当COMPRESS_OFFLINE为True时,这似乎只是一个问题.我将其设置为False,并且在初始请求期间创建的压缩文件正常工作,并且已经超过一个小时.但是,我更喜欢使用管理命令预压缩这些文件.