How to store an image as a Bytes field in cassandra database using cql engine?
Asked Answered
P

2

6

I want to store my website images in cassandra database! I must read and store bytes of image. Do you have a nice code for me? I'm using python2.7, django framework and cql-engine!

This is my code:

1- My Model:

from cqlengine import columns
from cqlengine.models import Model

class UserImage(Model):
    Email = columns.Text(primary_key=True)
    image=columns.Bytes(required=False)

2- My Form:

class UserImage(forms.Form):
    image=forms.ImageField()

3- My View:

from MainAPP.models import UserImage as UserImageModel
from MainAPP.forms import UsersForms

from django.http import HttpResponse
from cqlengine import connection
from PIL import Image

def UploadImage(request):
    if request.method == 'POST':
        form = UsersForms.UserImage(request.POST, request.FILES)
        if form.is_valid():
            try:
                image_data=Image.open(request.FILES['image'])
            except IOError:
                return HttpResponse("cannot upload %s"% request.FILES['image'].name)
            connection.setup(['127.0.0.1:9160'])
            UserImageModel.create(Email='[email protected]', image=image_data)
            return HttpResponse('Stored Successfully!')
    else:
        form= UsersForms.UserImage()
        return render_to_response('Users/uploadImage.html', {'form': form}, context_instance=RequestContext(request))

My Template:

{% block content %}
    <form enctype="multipart/form-data" method="post" action="">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Upload</button>
    </form>
{% endblock %}

My ERROR In Rendered Template:

AttributeError at /uploadImage
encode
Request Method: POST
Request URL:    http://127.0.0.1:8000/uploadImage
Django Version: 1.5.1
Exception Type: AttributeError
Exception Value:    encode

What is your idea? Please guide me...

Procathedral answered 17/7, 2013 at 20:48 Comment(1)
I personally try to avoid saving binary files (specifically media files like images, audio and video) in the database. I usually save the url to the image in the database, but the image data is saved somewhere like Amazon S3 with Cloudfront CDN to speed up the downloads for users. There is a django addon already for this django-storages.readthedocs.org/en/latestZak
P
0

My Edited View: We can store image in cassandra without using PIL:

def UploadImage(request):
    if request.method == 'POST':
        form = UsersForms.UserImage(request.POST, request.FILES)
        if form.is_valid():
            try:
                image_data=request.FILES['image'].read()
            except IOError:
                return HttpResponse("cannot convert %s"% request.FILES['image'].name)
            connection.setup(['127.0.0.1:9160'])
            UserImageModel.create(Email='[email protected]', image=image_data)
            return HttpResponse(request.FILES['image'].name)
    else:
        form= UsersForms.UserImage()
        return render_to_response('Users/uploadImage.html', {'form': form}, context_instance=RequestContext(request))
Procathedral answered 25/8, 2013 at 7:22 Comment(0)
S
2

If you look at the exception message you are getting, it says AttributeError: encode. This is telling you that somewhere in that code path, something is looking for an attribute (or most likely a method) called "encode" on some object and not finding it.

I suspect that you are not passing the right duck-typed object to cqlengine for UserImage.image. Does it know how to speak PIL Image objects? I doubt it. I bet cqlengine is looking for the typical python string method .encode (http://docs.python.org/2/library/stdtypes.html#str.encode). Instead of passing the PIL image, try passing just the raw bytes you receive from the form posting.

Stamata answered 17/8, 2013 at 3:57 Comment(0)
P
0

My Edited View: We can store image in cassandra without using PIL:

def UploadImage(request):
    if request.method == 'POST':
        form = UsersForms.UserImage(request.POST, request.FILES)
        if form.is_valid():
            try:
                image_data=request.FILES['image'].read()
            except IOError:
                return HttpResponse("cannot convert %s"% request.FILES['image'].name)
            connection.setup(['127.0.0.1:9160'])
            UserImageModel.create(Email='[email protected]', image=image_data)
            return HttpResponse(request.FILES['image'].name)
    else:
        form= UsersForms.UserImage()
        return render_to_response('Users/uploadImage.html', {'form': form}, context_instance=RequestContext(request))
Procathedral answered 25/8, 2013 at 7:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.