Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django + Boto + Python 3

How can I store my Django uploaded files on S3 using Python 3 on EC2 Amazon Linux? If I can't, how can I share uploaded files between 2 EC2 instances if I'm using ELB?

I tried to use the django-storages-py3 + boto#py3kport but it doesn't work, when I'm trying to upload files I get an exception: string expected bytes given

UPDATE:

This is how I'm using the 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

file - InMemoryUploadedFile: 1.png (image/png)

Traceback

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'

Upload using Django admin:

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
like image 429
user3164429 Avatar asked Nov 02 '22 06:11

user3164429


1 Answers

tinys3 - Quick and minimal S3 uploads for Python https://github.com/smore-inc/tinys3

like image 165
user3164429 Avatar answered Nov 15 '22 04:11

user3164429