python - Django + 博托 + Python 3

标签 python django amazon-web-services amazon-s3 boto

如何在 EC2 Amazon Linux 上使用 Python 3 将 Django 上传的文件存储在 S3 上? 如果不能,我如何在使用 ELB 的情况下在 2 个 EC2 实例之间共享上传的文件?

我尝试使用 django-storages-py3 + boto#py3kport 但它不起作用,当我尝试上传文件时出现异常:string expected bytes given

更新:

这就是我使用 django-storages-py3 + boto#py3kport 的方式

from django.core.files.storage import default_storage

fd = default_storage.open('%s/%s' % ('uploadstg', str(filename)), 'wb')
for chunk in file.chunks():
    fd.write(chunk)
fd.close()

fd - S3BotoStorageFile: uploadstg/a6d2532d-34c9-4793-9d43-e9a3e475fc6f.png

文件 - InMemoryUploadedFile: 1.png (image/png)

回溯

Environment:


Request Method: POST
Request URL: http://---/items/Tpp/create/

Django Version: 1.6.1
Python Version: 3.3.3
Installed Applications:
('django.contrib.admin',
 'haystack',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.sites',
 'modeltranslation',
 'south',
 'core',
 'storages',
 'appl')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'tpp.SiteUrlMiddleWare.SiteUrlMiddleWare')


Traceback:
File "/usr/local/bin/test/lib/python3.3/site-packages/django/core/handlers/base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/var/www/html/tpp/tppcenter/views.py" in get_item_form
  95.             com = form.save(request.user)
File "/var/www/html/tpp/tppcenter/forms.py" in save
  212.                 self._save_file(self.fields[title].initial, title, path_to_images)
File "/var/www/html/tpp/tppcenter/forms.py" in _save_file
  239.             fd.write(chunk)
File "/usr/local/bin/test/lib/python3.3/site-packages/django_storages-1.1.8-py3.3.egg/storages/backends/s3boto.py" in write
  161.         return super(S3BotoStorageFile, self).write(*args, **kwargs)

Exception Type: TypeError at /items/Tpp/create/
Exception Value: string argument expected, got 'bytes'

使用 Django 管理上传:

Environment:


Request Method: POST
Request URL: http://----/admin/core/user/1/

Django Version: 1.6.1
Python Version: 3.3.3
Installed Applications:
('django.contrib.admin',
 'haystack',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.sites',
 'modeltranslation',
 'south',
 'core',
 'storages',
 'appl')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'tpp.SiteUrlMiddleWare.SiteUrlMiddleWare')


Traceback:
File "/usr/local/bin/test/lib/python3.3/site-packages/django/core/handlers/base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/bin/test/lib/python3.3/site-packages/django/contrib/admin/options.py" in wrapper
  432.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "/usr/local/bin/test/lib/python3.3/site-packages/django/utils/decorators.py" in _wrapped_view
  99.                     response = view_func(request, *args, **kwargs)
File "/usr/local/bin/test/lib/python3.3/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  52.         response = view_func(request, *args, **kwargs)
File "/usr/local/bin/test/lib/python3.3/site-packages/django/contrib/admin/sites.py" in inner
  198.             return view(request, *args, **kwargs)
File "/usr/local/bin/test/lib/python3.3/site-packages/django/utils/decorators.py" in _wrapper
  29.             return bound_func(*args, **kwargs)
File "/usr/local/bin/test/lib/python3.3/site-packages/django/utils/decorators.py" in _wrapped_view
  99.                     response = view_func(request, *args, **kwargs)
File "/usr/local/bin/test/lib/python3.3/site-packages/django/utils/decorators.py" in bound_func
  25.                 return func(self, *args2, **kwargs2)
File "/usr/local/bin/test/lib/python3.3/site-packages/django/db/transaction.py" in inner
  339.                 return func(*args, **kwargs)
File "/usr/local/bin/test/lib/python3.3/site-packages/django/contrib/admin/options.py" in change_view
  1230.                 self.save_model(request, new_object, form, True)
File "/usr/local/bin/test/lib/python3.3/site-packages/django/contrib/admin/options.py" in save_model
  860.         obj.save()
File "/usr/local/bin/test/lib/python3.3/site-packages/django/db/models/base.py" in save
  545.                        force_update=force_update, update_fields=update_fields)
File "/usr/local/bin/test/lib/python3.3/site-packages/django/db/models/base.py" in save_base
  573.             updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/usr/local/bin/test/lib/python3.3/site-packages/django/db/models/base.py" in _save_table
  632.                       for f in non_pks]
File "/usr/local/bin/test/lib/python3.3/site-packages/django/db/models/base.py" in <listcomp>
  632.                       for f in non_pks]
File "/usr/local/bin/test/lib/python3.3/site-packages/django/db/models/fields/files.py" in pre_save
  252.             file.save(file.name, file, save=False)
File "/usr/local/bin/test/lib/python3.3/site-packages/django/db/models/fields/files.py" in save
  86.         self.name = self.storage.save(name, content)
File "/usr/local/bin/test/lib/python3.3/site-packages/django/core/files/storage.py" in save
  49.         name = self._save(name, content)
File "/usr/local/bin/test/lib/python3.3/site-packages/django_storages-1.1.8-py3.3.egg/storages/backends/s3boto.py" in _save
  392.         self._save_content(key, content, headers=headers)
File "/usr/local/bin/test/lib/python3.3/site-packages/django_storages-1.1.8-py3.3.egg/storages/backends/s3boto.py" in _save_content
  403.                                    rewind=True, **kwargs)
File "/usr/local/bin/test/src/boto/build/lib/boto/s3/key.py" in set_contents_from_file
  1241.                            chunked_transfer=chunked_transfer, size=size)
File "/usr/local/bin/test/src/boto/build/lib/boto/s3/key.py" in send_file
  726.                                  chunked_transfer=chunked_transfer, size=size)
File "/usr/local/bin/test/src/boto/build/lib/boto/s3/key.py" in _send_file_internal
  893.         if self.base64md5:
File "/usr/local/bin/test/src/boto/build/lib/boto/s3/key.py" in _get_base64md5
  177.             return binascii.b2a_base64(self.local_hashes['md5']).rstrip('\n')

Exception Type: TypeError at /admin/core/user/1/
Exception Value: Type str doesn't support the buffer API

最佳答案

tinys3 - Python 的快速和最小 S3 上传 https://github.com/smore-inc/tinys3

关于python - Django + 博托 + Python 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21250860/

相关文章:

asp.net - 使用 Asp.net 的亚马逊图书搜索 API

python - 映射到 numpy 数组,为什么要花费更多时间?

javascript - 在 JQuery append() 中映射 Django URL

python - 如何用数组干净地索引 numpy 数组(或任何其他支持加法的东西,以便它可以被抵消)

python - Sentry可以在乘客WSGI(dreamhost)下运行吗

django,名称 'IndexView' 未定义

amazon-web-services - 我不能在 aws fargate 中运行超过 5 个任务

amazon-web-services - 有没有办法跨用户和 EC2 实例角色重用 AWS IAM 权限策略?

python - python 3.7 包真的不需要 __init__.py 吗?

python - 如何从python中的绑定(bind)方法中提取值?