3

我已经按照这个方法来设置 django + S3。具体来说:

import os

# AWS credentials
AWS_STORAGE_BUCKET_NAME = os.environ['AWS_STORAGE_BUCKET_NAME']
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
# boto config
AWS_ACCESS_KEY_ID = os.environ['AWS_ACCESS_KEY_ID']
AWS_SECRET_ACCESS_KEY = os.environ['AWS_SECRET_ACCESS_KEY']
AWS_HEADERS = {  # see http://developer.yahoo.com/performance/rules.html#expires
    'Expires': 'Thu, 31 Dec 2099 20:00:00 GMT',
    'Cache-Control': 'max-age=94608000',
}

# For the static files
STATICFILES_LOCATION = 'static'
STATICFILES_STORAGE = 'myapp.custom_storages.StaticStorage'
STATIC_URL = 'https://%s/%s/' % (AWS_S3_CUSTOM_DOMAIN, STATICFILES_LOCATION)

# For the media files
MEDIAFILES_LOCATION = 'media'
DEFAULT_FILE_STORAGE = 'myapp.custom_storages.MediaStorage'
MEDIA_URL = 'https://%s/%s/' % (AWS_S3_CUSTOM_DOMAIN, MEDIAFILES_LOCATION)

我的自定义存储很简单S3BotoStorage

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


class StaticStorage(S3BotoStorage):
    location = settings.STATICFILES_LOCATION


class MediaStorage(S3BotoStorage):
    location = settings.MEDIAFILES_LOCATION

我希望这collectstatic将尊重此配置(如操作指南中所述),并使用myapp.custom_storages.StaticStorage将收集的静态文件上传到 S3。相反,它只使用本地文件系统。因为我有:

STATIC_ROOT = os.path.join(BASE_DIR, 'mycollectstatic')

(只是因为'static'那里对我来说似乎太混乱了),我可以清楚地看到:

» python manage.py collectstatic

You have requested to collect static files at the destination
location as specified in your settings:

    /absolute-path/mycollectstatic

This will overwrite existing files!
Are you sure you want to do this?

Type 'yes' to continue, or 'no' to cancel: 

因此,collectstatic即使. 这是预期的吗?STATIC_ROOTSTATICFILES_STORAGE = 'myapp.custom_storages.StaticStorage'

STATIC_ROOT使用另一个时我应该进行不同的配置STATICFILES_STORAGE吗?这是在哪里记录的?

4

0 回答 0