collecstatic 不推送到文件 S3
collecstatic does not push to files S3
编辑:
我发现从我的 settings.py 文件中删除 import django_heroku
允许我将我的静态文件推送到我的 AWS 存储桶。当我取消注释 import django_heroku
时,collectstatic
然后将我的文件推送到 staticfiles
文件夹。
manage.py collectstatic
与 #import django_heroku
:
You have requested to collect static files at the destination location as specified in your settings.
maange.py collectstatic
与 import django_heroku
:
You have requested to collect static files at the destination
location as specified in your settings:
/path/to/project/staticfiles
我不知道这是为什么,也不知道如何解决。现在的问题是:如何 运行 在 Heroku 上为我的 django 应用程序收集静态信息?或者我是否需要 运行 我的 heroku 实例与 nostatic? (例如 runserver --nostatic
)
问题:
每当我 运行 python manage.py collectstatic
我所有的静态文件都放在 local 'staticfiles' 文件夹中。我已经设置 STATICFILES_STORAGE = myapp.aws.utils.StaticRootS3Boto3Storage
但是文件总是转到 'myapp/staticfiles'.
Settings.py:
AWS_ACCESS_KEY_ID = config('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = config('AWS_SECRET_ACCESS_KEY')
AWS_FILE_EXPIRE = 200
AWS_PRELOAD_METADATA = True
AWS_QUERYSTRING_AUTH = True
DEFAULT_FILE_STORAGE = 'myapp.aws.utils.MediaRootS3Boto3Storage'
STATICFILES_STORAGE = 'myapp.aws.utils.StaticRootS3Boto3Storage'
AWS_STORAGE_BUCKET_NAME = config('AWS_STORAGE_BUCKET_NAME')
S3DIRECT_REGION='us-east-1'
AWS_S3_URL = '//s3.amazonaws.com/%s' % AWS_STORAGE_BUCKET_NAME
MEDIA_URL = 'http://s3.amazonaws.com/%s/media/' % AWS_STORAGE_BUCKET_NAME
MEDIA_ROOT = MEDIA_URL
STATIC_URL = AWS_S3_URL + '/static/'
STATICFILES_LOCATION = STATIC_URL
MEDIAFILES_LOCATION = 'media'
ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/'
AWS_HEADERS = {
'Expires': 'Thu, 31 Dec 2099 20:00:00 GMT',
'CacheControl': 'max-age=94608000',
}
myapp/aws/utils.py:
from storages.backends.s3boto3 import S3Boto3Storage
StaticRootS3Boto3Storage = lambda: S3Boto3Storage(location='static')
MediaRootS3Boto3Storage = lambda: S3Boto3Storage(location='media')
项目结构:
myproject/
|-- myapp/
| |-- aws/
| | |-- __init__.py
| | |-- utils.py
| |-- __init__.py
| |-- settings.py
| |-- urls.py
| +-- wsgi.py
+-- manage.py
|-- static/
| |-- scss
| |-- css
| |-- js
| |-- images
|-- staticfiles/
| |-- **ALL STATIC FILES END UP HERE**
备注:
- 改变
STATICFILES_STORAGE
似乎对输出没有影响
python manage.py collectstatic
结果:
You have requested to collect static files at the destination
location as specified in your settings:
/Users/nickmancini/Development/myapp/staticfiles
我没有收到任何错误消息。所有文件都在本地 staticfiles 文件夹中成功创建。
媒体文件已成功上传到我的 S3 存储桶,但静态文件总是上传 /Users/me/myproject/myapp/staticfiles
根据文档,设置后 STATICFILES_STORAGES "all you have to do is run collectstatic and your static files would be pushed through your storage package up to S3"
问题:
- 我可以提供哪些其他有用的信息?
我查阅过的资源:
- https://docs.djangoproject.com/en/2.0/howto/static-files/deployment/#serving-static-files-from-a-cloud-service-or-cdn
- https://simpleisbetterthancomplex.com/tutorial/2017/08/01/how-to-setup-amazon-s3-in-a-django-project.html
- https://www.codingforentrepreneurs.com/blog/s3-static-media-files-for-django/
对于遇到相同问题的任何人,我的解决方案是:django_heroku.settings(locals(), staticfiles=False)
编辑:
我发现从我的 settings.py 文件中删除 import django_heroku
允许我将我的静态文件推送到我的 AWS 存储桶。当我取消注释 import django_heroku
时,collectstatic
然后将我的文件推送到 staticfiles
文件夹。
manage.py collectstatic
与 #import django_heroku
:
You have requested to collect static files at the destination location as specified in your settings.
maange.py collectstatic
与 import django_heroku
:
You have requested to collect static files at the destination location as specified in your settings: /path/to/project/staticfiles
我不知道这是为什么,也不知道如何解决。现在的问题是:如何 运行 在 Heroku 上为我的 django 应用程序收集静态信息?或者我是否需要 运行 我的 heroku 实例与 nostatic? (例如 runserver --nostatic
)
问题:
每当我 运行 python manage.py collectstatic
我所有的静态文件都放在 local 'staticfiles' 文件夹中。我已经设置 STATICFILES_STORAGE = myapp.aws.utils.StaticRootS3Boto3Storage
但是文件总是转到 'myapp/staticfiles'.
Settings.py:
AWS_ACCESS_KEY_ID = config('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = config('AWS_SECRET_ACCESS_KEY')
AWS_FILE_EXPIRE = 200
AWS_PRELOAD_METADATA = True
AWS_QUERYSTRING_AUTH = True
DEFAULT_FILE_STORAGE = 'myapp.aws.utils.MediaRootS3Boto3Storage'
STATICFILES_STORAGE = 'myapp.aws.utils.StaticRootS3Boto3Storage'
AWS_STORAGE_BUCKET_NAME = config('AWS_STORAGE_BUCKET_NAME')
S3DIRECT_REGION='us-east-1'
AWS_S3_URL = '//s3.amazonaws.com/%s' % AWS_STORAGE_BUCKET_NAME
MEDIA_URL = 'http://s3.amazonaws.com/%s/media/' % AWS_STORAGE_BUCKET_NAME
MEDIA_ROOT = MEDIA_URL
STATIC_URL = AWS_S3_URL + '/static/'
STATICFILES_LOCATION = STATIC_URL
MEDIAFILES_LOCATION = 'media'
ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/'
AWS_HEADERS = {
'Expires': 'Thu, 31 Dec 2099 20:00:00 GMT',
'CacheControl': 'max-age=94608000',
}
myapp/aws/utils.py:
from storages.backends.s3boto3 import S3Boto3Storage
StaticRootS3Boto3Storage = lambda: S3Boto3Storage(location='static')
MediaRootS3Boto3Storage = lambda: S3Boto3Storage(location='media')
项目结构:
myproject/
|-- myapp/
| |-- aws/
| | |-- __init__.py
| | |-- utils.py
| |-- __init__.py
| |-- settings.py
| |-- urls.py
| +-- wsgi.py
+-- manage.py
|-- static/
| |-- scss
| |-- css
| |-- js
| |-- images
|-- staticfiles/
| |-- **ALL STATIC FILES END UP HERE**
备注:
- 改变
STATICFILES_STORAGE
似乎对输出没有影响 python manage.py collectstatic
结果:You have requested to collect static files at the destination location as specified in your settings:
/Users/nickmancini/Development/myapp/staticfiles
我没有收到任何错误消息。所有文件都在本地 staticfiles 文件夹中成功创建。
媒体文件已成功上传到我的 S3 存储桶,但静态文件总是上传 /Users/me/myproject/myapp/staticfiles
根据文档,设置后 STATICFILES_STORAGES "all you have to do is run collectstatic and your static files would be pushed through your storage package up to S3"
问题:
- 我可以提供哪些其他有用的信息?
我查阅过的资源:
- https://docs.djangoproject.com/en/2.0/howto/static-files/deployment/#serving-static-files-from-a-cloud-service-or-cdn
- https://simpleisbetterthancomplex.com/tutorial/2017/08/01/how-to-setup-amazon-s3-in-a-django-project.html
- https://www.codingforentrepreneurs.com/blog/s3-static-media-files-for-django/
对于遇到相同问题的任何人,我的解决方案是:django_heroku.settings(locals(), staticfiles=False)