gpt4 book ai didi

python - Django-compressor:如何写入 S3,从 CloudFront 读取?

转载 作者:IT老高 更新时间:2023-10-28 21:06:13 25 4
gpt4 key购买 nike

我想从 CloudFront 提供我的压缩 CSS/JS(它们位于 S3 上),但无法通过 settings.py 中的压缩器设置来解决此问题,我有以下内容:

    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

尽管有 COMPRESS_URL,但我的文件正在从我的 s3 存储桶中读取:
<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 读取它。这可能吗?

最佳答案

我围绕 boto 提供的后端编写了一个包装存储后端

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 文件在哪里...

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'

关于python - Django-compressor:如何写入 S3,从 CloudFront 读取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8688815/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com