将django static部署到亚马逊s3

时间:2014-08-14 05:34:50

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

请你帮忙运行收集静态到亚马逊s3?

这是我的配置文件:

05_collect_admin_static:
    command: "python manage.py collectstatic --noinput"
    leader_only: true

在设置文件中 我在用 django-storages == 1.1.8和boto == 2.32.1

INSTALLED_APPS += ('storages',)
AWS_ACCESS_KEY_ID = os.environ['AWS_ACCESS_KEY_ID']
AWS_SECRET_ACCESS_KEY = os.environ['AWS_SECRET_KEY']
AWS_STORAGE_BUCKET_NAME = os.environ['AWS_STORAGE_BUCKET_NAME']

DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage'

S3_URL = 'http://%s.s3.amazonaws.com/' % AWS_STORAGE_BUCKET_NAME
STATIC_URL = S3_URL

这是我的日志:

/var/log/cfn-init.log

   execute_from_command_line(sys.argv)
  File "/opt/python/run/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
    utility.execute()
  File "/opt/python/run/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/opt/python/run/venv/lib/python2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/opt/python/run/venv/lib/python2.7/site-packages/django/core/management/base.py", line 285, in execute
    output = self.handle(*args, **options)
  File "/opt/python/run/venv/lib/python2.7/site-packages/django/core/management/base.py", line 415, in handle
    return self.handle_noargs(**options)
  File "/opt/python/run/venv/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 173, in handle_noargs
    collected = self.collect()
  File "/opt/python/run/venv/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 112, in collect
    handler(path, prefixed_path, storage)
  File "/opt/python/run/venv/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 317, in copy_file
    self.storage.save(prefixed_path, source_file)
  File "/opt/python/run/venv/lib/python2.7/site-packages/django/core/files/storage.py", line 49, in save
    name = self._save(name, content)
  File "/opt/python/run/venv/lib/python2.7/site-packages/storages/backends/s3boto.py", line 392, in _save
    self._save_content(key, content, headers=headers)
  File "/opt/python/run/venv/lib/python2.7/site-packages/storages/backends/s3boto.py", line 403, in _save_content
    rewind=True, **kwargs)
  File "/opt/python/run/venv/lib/python2.7/site-packages/boto/s3/key.py", line 1286, in set_contents_from_file
    chunked_transfer=chunked_transfer, size=size)
  File "/opt/python/run/venv/lib/python2.7/site-packages/boto/s3/key.py", line 746, in send_file
    chunked_transfer=chunked_transfer, size=size)
  File "/opt/python/run/venv/lib/python2.7/site-packages/boto/s3/key.py", line 944, in _send_file_internal
    query_args=query_args
  File "/opt/python/run/venv/lib/python2.7/site-packages/boto/s3/connection.py", line 664, in make_request
    retry_handler=retry_handler
  File "/opt/python/run/venv/lib/python2.7/site-packages/boto/connection.py", line 1053, in make_request
    retry_handler=retry_handler)
  File "/opt/python/run/venv/lib/python2.7/site-packages/boto/connection.py", line 923, in _mexe
    request.body, request.headers)
  File "/opt/python/run/venv/lib/python2.7/site-packages/boto/s3/key.py", line 840, in sender
    http_conn.send(chunk)
  File "/usr/lib64/python2.7/httplib.py", line 805, in send
    self.sock.sendall(data)
  File "/usr/lib64/python2.7/ssl.py", line 229, in sendall
    v = self.send(data[count:])
  File "/usr/lib64/python2.7/ssl.py", line 198, in send
    v = self._sslobj.write(data)
socket.error: [Errno 104] Connection reset by peer

感谢。是否与许可问题有关?我正在使用aws弹性豆茎。再次感谢。

2 个答案:

答案 0 :(得分:2)

以下是为我修复的内容(settings.py):

AWS_S3_HOST = 's3-us-west-2.amazonaws.com'

通常,boto会使用默认区域。如果您的存储桶不在默认区域,则AWS将使用重定向进行响应(并且boto不会遵循重定向)。

如果没有此设置,无论如何都会上传较小的文件 - 可能只为较大的文件发出重定向?

答案 1 :(得分:1)

似乎我终于找到了解决方案。

这是设置文件:

AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_KEY')
AWS_STORAGE_BUCKET_NAME = os.environ.get('AWS_STORAGE_BUCKET_NAME')

STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'

STATIC_URL = 'http://' + AWS_STORAGE_BUCKET_NAME + '.s3.amazonaws.com/'
ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/'

我犯的错误是:

wrong:
  - option_name: AWS_STORAGE_BUCKET_NAME
    value: bucket_name.s3-website-eu-west-1.amazonaws.com

correct:
  - option_name: AWS_STORAGE_BUCKET_NAME
    value: bucket_name
傻我。我花了两天时间才发现这个问题......