AWS Elastic Beanstalk& Django 1.5:S3的Collectstatic失败了

时间:2013-12-04 17:39:04

标签: django amazon-web-services amazon-s3 elastic-beanstalk

正如标题所说,在尝试将我的项目部署到AWS EB时,如果与S3一起使用,则collectstatic会失败。它在指定的存储桶中创建一个文件夹,上传2个文件,然后突然返回Command 02_collectstatic failed

我使用以下代码:

的myapp / settings.py

AWS_ACCESS_KEY_ID = '---key-ID---'
AWS_SECRET_ACCESS_KEY = '---key---'
AWS_STORAGE_BUCKET_NAME = '---bucket---'

DEFAULT_FILE_STORAGE = 'myapp.s3utils.MediaRootS3BotoStorage'
STATICFILES_STORAGE = 'myapp.s3utils.StaticRootS3BotoStorage'
S3_URL = 'http://s3.amazonaws.com/%s' % AWS_STORAGE_BUCKET_NAME
MEDIA_ROOT = '/media/' 
MEDIA_URL = S3_URL + MEDIA_ROOT
STATIC_ROOT = '/static/'
STATIC_URL = S3_URL + STATIC_ROOT

的myapp / s3utils.py

from storages.backends.s3boto import S3BotoStorage

StaticRootS3BotoStorage = lambda: S3BotoStorage(location='static')
MediaRootS3BotoStorage  = lambda: S3BotoStorage(location='media')

.ebextensions / myapp.config

container_commands:
  01_syncdb:    
    command: "django-admin.py syncdb --noinput"
    leader_only: true           
  02_collectstatic:
    command: "django-admin.py collectstatic --noinput"

option_settings:
  - namespace: aws:elasticbeanstalk:container:python
    option_name: WSGIPath
    value: myapp/wsgi.py 
  - namespace: aws:elasticbeanstalk:container:python:staticfiles
    option_name: /static/
    value: myapp/static/
  - option_name: DJANGO_SETTINGS_MODULE
    value: myapp.settings
  - option_name: AWS_SECRET_KEY
    value: ---key---
  - option_name: AWS_ACCESS_KEY_ID
    value: ---key-ID---

错误日志显示

2013-12-04 17:29:42,887 [DEBUG] Running command 02_collectstatic
2013-12-04 17:29:42,888 [DEBUG] Generating defaults for command 02_collectstatic
<<<

2013-12-04 17:29:42,983 [DEBUG] No test for command 02_collectstatic
2013-12-04 17:30:38,186 [ERROR] Command 02_collectstatic (django-admin.py collectstatic --noinput) failed
2013-12-04 17:30:38,187 [DEBUG] Command 02_collectstatic output: error: [Errno 104] Connection reset by peer
Copying '/opt/python/ondeck/app/myapp/staticbase/js/skel.min.js'
Copying '/opt/python/ondeck/app/myapp/staticbase/js/config.js'
Copying '/opt/python/ondeck/app/myapp/staticbase/js/jquery.min.js'

2013-12-04 17:30:38,188 [ERROR] Error encountered during build of postbuild_0_myapp_git_23c2abc94089130640397058982b7612214c31f5_1386178136709__ebextensions_myapp_config: Command 02_collectstatic failed
Traceback (most recent call last):
  File "/usr/lib/python2.6/site-packages/cfnbootstrap/construction.py", line 511, in run_config
    CloudFormationCarpenter(config, self._auth_config).build(worklog)
  File "/usr/lib/python2.6/site-packages/cfnbootstrap/construction.py", line 247, in build
    changes['commands'] = CommandTool().apply(self._config.commands)
  File "/usr/lib/python2.6/site-packages/cfnbootstrap/command_tool.py", line 113, in apply
    raise ToolError(u"Command %s failed" % name)
ToolError: Command 02_collectstatic failed
2013-12-04 17:30:38,189 [ERROR] Unhandled exception during build: Command 02_collectstatic failed
Traceback (most recent call last):
  File "/opt/aws/bin/cfn-init", line 122, in <module>
    worklog.build(detail.metadata, configSets)
  File "/usr/lib/python2.6/site-packages/cfnbootstrap/construction.py", line 117, in build
    Contractor(metadata).build(configSets, self)
  File "/usr/lib/python2.6/site-packages/cfnbootstrap/construction.py", line 502, in build
    self.run_config(config, worklog)
  File "/usr/lib/python2.6/site-packages/cfnbootstrap/construction.py", line 511, in run_config
    CloudFormationCarpenter(config, self._auth_config).build(worklog)
  File "/usr/lib/python2.6/site-packages/cfnbootstrap/construction.py", line 247, in build
    changes['commands'] = CommandTool().apply(self._config.commands)
  File "/usr/lib/python2.6/site-packages/cfnbootstrap/command_tool.py", line 113, in apply
    raise ToolError(u"Command %s failed" % name)
ToolError: Command 02_collectstatic failed
2013-12-04 17:33:00,210 [DEBUG] CloudFormation client initialized with endpoint https://cloudformation.us-west-2.amazonaws.com
2013-12-04 17:33:00,210 [DEBUG] Describing resource AWSEBAutoScalingGroup in stack arn:aws:cloudformation:us-west-2:298535049725:stack/awseb-e-m7uf3bwq2h-stack/1118d580-5cd0-11e3-8724-500150b35218
2013-12-04 17:33:00,405 [DEBUG] Not setting a reboot trigger as scheduling support is not available
2013-12-04 17:33:00,419 [INFO] Running configSets: InfoTask-TailLogs
2013-12-04 17:33:00,420 [INFO] Running configSet InfoTask-TailLogs
2013-12-04 17:33:00,421 [INFO] Running config InfoTask-TailLogs
2013-12-04 17:33:00,421 [DEBUG] No packages specified
2013-12-04 17:33:00,422 [DEBUG] No groups specified
2013-12-04 17:33:00,422 [DEBUG] No users specified
2013-12-04 17:33:00,422 [DEBUG] No sources specified
2013-12-04 17:33:00,422 [DEBUG] No files specified
2013-12-04 17:33:00,422 [DEBUG] Running command taillogs
2013-12-04 17:33:00,423 [DEBUG] No test for command taillogs

因此,似乎复制了skel.min.js和config.js,并且在尝试复制jquery.min.js时发生错误。知道什么可能导致这个错误吗?

2 个答案:

答案 0 :(得分:2)

@barrigaj,是的,我找到了解决方案。我只需更改顺序(MEDIA_ROOT和STATIC_ROOT需要高于DEFAULT_FILE_STORAGE和STATICFILES_STORAGE)。

MEDIA_ROOT = '/media/' 
STATIC_ROOT = '/static/'
DEFAULT_FILE_STORAGE = 'myapp.s3utils.MediaRootS3BotoStorage'
STATICFILES_STORAGE = 'myapp.s3utils.StaticRootS3BotoStorage'
S3_URL = 'http://s3.amazonaws.com/%s' % AWS_STORAGE_BUCKET_NAME
MEDIA_URL = S3_URL + MEDIA_ROOT
STATIC_URL = S3_URL + STATIC_ROOT

答案 1 :(得分:0)

我认为答案比这简单得多。这对我来说是在AWS Elastic Beanstalk上运行的:

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
)

DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
AWS_ACCESS_KEY_ID = os.environ['AWS_ACCESS_KEY_ID']
AWS_SECRET_ACCESS_KEY = os.environ['AWS_SECRET_KEY']

AWS密钥是从容器配置文件传入的,我根本没有设置STATIC_ROOTSTATIC_URL。也不需要myapp.s3utils文件。这些细节由存储系统处理。这里的技巧是我需要在我的模板中正确且动态地引用这个未知路径。例如:

<link rel="icon" href="{% static "img/favicon.ico" %}">

这就是我在~/Projects/my_app/project/my_app/static/img/中解决本地(预部署)生活的网站的方式。

当然,我有一个单独的local_settings.py文件,用于在开发环境中本地访问这些内容。我不得不进行大量的实验和阅读以找到这个解决方案,并且它始终如一地工作而没有错误。