使用boto3和存储将django app的静态文件上传到s3

时间:2016-12-11 16:33:21

标签: python django amazon-s3 boto3 django-staticfiles

我知道这个问题已被问过很多次,但我还是无法让它发挥作用。我的Env配置:

  • Python 2.7
  • Django 1.9.5

在我的requrements.txt中,我有以下依赖

  • boto3 == 1.4.2
  • Django的存储器== 1.5.1

我在我的INSTALLED_APPS中添加了storages。这些是我的设置参数。

AWS_ACCESS_KEY_ID='****'
AWS_SECRET_ACCESS_KEY='****'
AWS_STORAGE_BUCKET_NAME='****'
AWS_AUTO_CREATE_BUCKET = False
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
STATICFILES_STORAGE = DEFAULT_FILE_STORAGE
AWS_LOCATION = 'static/'

当我运行./manage.py collectstatic命令时,我收到以下错误:

File "/home/me/myproj/local/lib/python2.7/site-packages/storages/backends/s3boto.py", line 23, in <module>
    raise ImproperlyConfigured("Could not load Boto's S3 bindings.\n"
django.core.exceptions.ImproperlyConfigured: Could not load Boto's S3 bindings.
See https://github.com/boto/boto

任何想法为什么?我没有正确配置什么?

3 个答案:

答案 0 :(得分:7)

当我写评论时,我终于想出了解决方案: 在您的settings.py中使用:

STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

而不是:

STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage'

对于有关boto3的更好的文档,libary上还有一个未解决的问题: https://github.com/jschneier/django-storages/issues/229

我在django-storage的源代码中找到了解决方案,你可以在这里找到: https://github.com/jschneier/django-storages/blob/master/storages/backends/s3boto3.py

答案 1 :(得分:0)

如果你看一下django-storages代码(https://github.com/jschneier/django-storages/blob/master/storages/backends/s3boto.py),当django-storages无法完成grep语句到import时,就会抛出此错误。我的猜测是你正在使用一个非常非常老的版本的boto。尝试使用以下代码将boto升级到版本2.40+

boto

答案 2 :(得分:0)

DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'

以来

你必须安装boto

pip install boto