更新:错误不是由 django-storages 引起的,而是由 django-heroku 引起的
似乎 django-heroku 覆盖了一些导致这种行为的变量。请看下面我的回答。
我一直在使用 S3 作为其他 Django 应用程序的存储服务,但是对于一个新项目,Django 拒绝选择正确的后端并继续将文件放在我的本地文件系统上而不是上传。
确切地说,在我安装 boto3 并调整settings.py
,然后运行之后python manage.py collectstatic
,该命令会继续将静态文件移动到,<my_project_path>/staticfiles
而不是开始上传到 S3。
这是的输出python manage.py collectstatic
:
You have requested to collect static files at the destination
location as specified in your settings:
<my_project_path>/staticfiles
This will overwrite existing files!
Are you sure you want to do this?
Type 'yes' to continue, or 'no' to cancel:
我的设置:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
# Vendor
'storages',
'allauth',
'allauth.account',
'allauth.socialaccount',
'sass_processor',
'crispy_forms',
[..]
]
# static & media files
USE_S3 = config('USE_S3', cast=bool, default=True)
AWS_ACCESS_KEY_ID = config('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = config('AWS_SECRET_ACCESS_KEY')
AWS_DEFAULT_ACL = None
AWS_STORAGE_BUCKET_NAME = config('AWS_STORAGE_BUCKET')
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
AWS_S3_OBJECT_PARAMETERS = {
'CacheControl': 'max-age=86400',
}
AWS_LOCATION = 'static'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
if USE_S3:
MEDIA_URL = 'https://%s/%s/' % (AWS_S3_CUSTOM_DOMAIN, 'media')
DEFAULT_FILE_STORAGE = '<my_app>.storage_backends.MediaStorage'
STATIC_URL = 'https://%s/%s/' % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION)
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
else:
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'sass_processor.finders.CssFinder',
]
我的要求.txt:
...
boto3==1.11.12
botocore==1.14.15
django-storages==1.9.1
jmespath==0.9.4
s3transfer==0.3.3
...
我尝试从上传工作的其他项目复制设置,重新检查django-storages
文档,甚至安装了 boto3 等的确切版本以排除由于较新版本而导致的错误,检查我settings.py
是否在某个地方忘记了冲突设置,但我只是无法弄清楚为什么 Django 甚至不尝试上传。
非常感谢任何指针!