collecstatic不会推送到文件S3 [英] collecstatic does not push to files S3

查看:147
本文介绍了collecstatic不会推送到文件S3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现从我的settings.py文件中删除import django_heroku允许我将静态文件推送到我的AWS存储桶.当我取消注释import django_heroku时,collectstatic然后将我的文件推送到staticfiles文件夹.

I have found that removing import django_heroku from my settings.py file allows me to push my static files to my AWS bucket. When I uncomment import django_heroku, collectstatic then pushes my files to the staticfiles folder.

manage.py collectstatic#import django_heroku:

您已请求按照设置中的指定位置在目标位置收集静态文件.

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

maange.py collectstaticimport django_heroku:

您已请求在目标位置收集静态文件 在您的设置中指定的位置: /path/to/project/staticfiles

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

我不知道这是为什么或如何解决.现在的问题是:如何在Heroku上为我的Django应用程序运行collectstatic?还是我需要使用nostatic运行我的heroku实例? (例如runserver --nostatic)

I don't know why this is or how to fix it. Now the question is: How can I run collectstatic for my django app on Heroku? Or do I need to run my heroku instance with nostatic? (e.g. runserver --nostatic)

问题:

每当我运行python manage.py collectstatic时,我所有的静态文件都会放在 local 'staticfiles'文件夹中.我已经设置了STATICFILES_STORAGE = myapp.aws.utils.StaticRootS3Boto3Storage,但是文件始终转到"myapp/staticfiles".

Whenever I run python manage.py collectstatic all my static files are placed into a local 'staticfiles' folder. I have set STATICFILES_STORAGE = myapp.aws.utils.StaticRootS3Boto3Storage however the files always go to '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结果为:

  • Changing STATICFILES_STORAGE seems to have no effect on the output
  • python manage.py collectstatic results in:

You have requested to collect static files at the destination location as specified in your settings:
/Users/nickmancini/Development/myapp/staticfiles

You have requested to collect static files at the destination location as specified in your settings:
/Users/nickmancini/Development/myapp/staticfiles

我没有收到任何错误消息.所有文件均已在本地staticfiles文件夹中成功创建.

I do not get any error messages. All files are successfully created in the local staticfiles folder.

媒体文件已成功上传到我的S3存储桶中,但静态文件始终位于/Users/me/myproject/myapp/staticfiles

Media files are successfully uploaded to my S3 bucket, but static files always go /Users/me/myproject/myapp/staticfiles

根据文档,设置STATICFILES_STORAGES后,您要做的就是运行collectstatic,并且您的静态文件将通过存储包推送到S3"

According to the docs, after setting 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)

For anyone facing the same problem, the solution for me was: django_heroku.settings(locals(), staticfiles=False)

在github问题上也有详细介绍.

这篇关于collecstatic不会推送到文件S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆