我正在尝试找到一种方法将我的Django静态文件上传到Amazon S3存储桶。
在settings.py
我有:
AWS_STORAGE_BUCKET_NAME = 'XXXXX'
AWS_ACCESS_KEY_ID = 'AXXXXXA'
AWS_SECRET_ACCESS_KEY = 'DXXXXXXXXX6'
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
STATICFILES_LOCATION = 'static'
STATICFILES_STORAGE = 'custom_storages.StaticStorage'
STATIC_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, STATICFILES_LOCATION)
我使用EB控制面板上传功能将我的Django代码作为zip文件上传到AWS EB实例。
确保将静态文件(在static /目录中)复制到S3存储桶的建议方法是什么?
答案 0 :(得分:0)
我会尝试做这样的设置:
<强> settings.py:强>
STATICFILES_DIRS = (
os.path.join('path', 'to_your', 'files'),
)
AWS_ACCESS_KEY_ID = 'AXXXXXA'
AWS_SECRET_ACCESS_KEY = 'DXXXXXXXXX6'
AWS_STORAGE_BUCKET_NAME = 'XXXXX'
STATICFILES_STORAGE = 'proj.s3utils.StaticRootS3BotoStorage' # static files
STATIC_S3_PATH = 'media/'
DEFAULT_FILE_STORAGE = 'proj.s3utils.MediaRootS3BotoStorage' # media uploads
DEFAULT_S3_PATH = 'static/'
S3_URL = '//{}.s3.amazonaws.com/'.format(AWS_STORAGE_BUCKET_NAME)
MEDIA_URL = S3_URL + STATIC_S3_PATH
STATIC_URL = S3_URL + DEFAULT_S3_PATH
MEDIA_ROOT = '/home/ubuntu/domain.com/proj/static/media' # assuming this is your layout
STATIC_ROOT = '/home/ubuntu/domain.com/proj/static/static' # assuming this is your layout
AWS_PRELOAD_METADATA = True
AWS_S3_SECURE_URLS = True
date_three_months_later = datetime.date.today() + datetime.timedelta(3 * 365 / 12)
expires = date_three_months_later.strftime('%A, %d %B %Y 20:00:00 EST')
AWS_HEADERS = {
'Expires': expires,
'Cache-Control': 'max-age=31536000', # 365 days
}
<强> s3utils.py:强>
确保您安装了django-storages
和boto
from storages.backends.s3boto import S3BotoStorage
StaticRootS3BotoStorage = lambda: S3BotoStorage(location='static')
MediaRootS3BotoStorage = lambda: S3BotoStorage(location='media')
这应该确保一切都进入你的S3桶。