collecstatic does not push to files S3

℡╲_俬逩灬. 提交于 2020-05-10 03:34:20

问题


EDIT:

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 with #import django_heroku:

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

maange.py collectstatic with import django_heroku:

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

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)

Problem:

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')

Project Structure:

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** 

Notes:

  • 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

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

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

  • 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"

Questions:

  • What other information can I provide that would be helpful?

Resources I've Consulted:

  • 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/

回答1:


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

which is also detailed at this github issue.



来源:https://stackoverflow.com/questions/49742714/collecstatic-does-not-push-to-files-s3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!