Django的COM pressor:如何写到S3,从CloudFront的阅读?写到、COM、Django、pressor

2023-09-11 08:26:12 作者:贱人话多、小心闪到舌头

我要为我的COM pressed CSS / JS从CloudFront的(他们住在S3),但我无法工作,如何通过在settings.py的COM pressor设​​置做到这一点,我有如下:

I want to serve my compressed CSS/JS from CloudFront (they live on S3), but am unable to work out how to do it via the compressor settings in settings.py, I have the following:

    COMPRESS_OFFLINE = True 
    COMPRESS_URL = 'http://static.example.com/' #same as STATIC_URL, so unnecessary, just here for simplicity
    COMPRESS_STORAGE = 'my_example_dir.storage.CachedS3BotoStorage' #subclass suggested in [docs][1]
    COMPRESS_OUTPUT_DIR = 'compressed_static'
    COMPRESS_ROOT = '/home/dotcloud/current/static/' #location of static files on server

尽管COM preSS_URL,正从我的S3存储读取我的文件: <链接相对=样式 href="https://example.s3.amazonaws.com/com$p$pssed_static/css/e0684a1d5c25.css?Signature=blahblahblah;Expires=farfuture;AWSAccessKeyId=blahblahblah" TYPE =文本/ CSS/>

Despite the COMPRESS_URL, my files are being read from my s3 bucket: <link rel="stylesheet" href="https://example.s3.amazonaws.com/compressed_static/css/e0684a1d5c25.css?Signature=blahblahblah;Expires=farfuture;AWSAccessKeyId=blahblahblah" type="text/css" />

我想这个问题是我想要写入文件到S3,但CloudFront的阅读。这可能吗?

I guess the issue is I want to write the file to S3, but read it from CloudFront. Is this possible?

推荐答案

我写了一个包装存储后端围绕由博托提供了一个

I wrote a wrapper storage backend around the one provided by boto

的myapp / storage_backends.py:

myapp/storage_backends.py:

import urlparse
from django.conf import settings
from storages.backends.s3boto import S3BotoStorage

def domain(url):
    return urlparse.urlparse(url).hostname    

class MediaFilesStorage(S3BotoStorage):
    def __init__(self, *args, **kwargs):
        kwargs['bucket'] = settings.MEDIA_FILES_BUCKET
        kwargs['custom_domain'] = domain(settings.MEDIA_URL)
        super(MediaFilesStorage, self).__init__(*args, **kwargs)

class StaticFilesStorage(S3BotoStorage):
    def __init__(self, *args, **kwargs):
        kwargs['bucket'] = settings.STATIC_FILES_BUCKET
        kwargs['custom_domain'] = domain(settings.STATIC_URL)
        super(StaticFilesStorage, self).__init__(*args, **kwargs)

在哪里我的settings.py文件有...

Where my settings.py file has...

STATIC_FILES_BUCKET = "myappstatic"
MEDIA_FILES_BUCKET = "myappmedia"
STATIC_URL = "http://XXXXXXXX.cloudfront.net/"
MEDIA_URL = "http://XXXXXXXX.cloudfront.net/"

DEFAULT_FILE_STORAGE = 'myapp.storage_backends.MediaFilesStorage'
COMPRESS_STORAGE = STATICFILES_STORAGE = 'myapp.storage_backends.StaticFilesStorage'
 
精彩推荐