Reputation: 781
Hi I am developing IOS application. I also use JSON web services. and I also use Google App Engine for python. I'm really new to python and google app engine. I can not figure out this problem;
I am sending base64 image and other infos in json. I want to save other infos to database(GQL) and save to image google file folder (Blob Store) and this photo url save with other infos in google database.
Can you help me thanx
UPDATED
import cgi
import datetime
import time
import urllib
import wsgiref.handlers
import os, urllib2, re, base64
import simplejson as json
from google.appengine.ext import db
from google.appengine.api import users,images, files
from google.appengine.ext import webapp
from google.appengine.ext import blobstore
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers
import logging
class PanoMessages(db.Model):
first_name=db.StringProperty();
last_name=db.StringProperty();
msg_text=db.TextProperty();
photo_url=blobstore.BlobReferenceProperty();
class IOSDeneme(webapp.RequestHandler):
def post(self):
received_content = self.request.body;
try:
decoded_json = json.loads(received_content);
panoMsg=PanoMessages();
panoMsg.photo_url=save_image_to_blobstore(decoded_json["picture"],"image/png");
panoMsg.first_name=decoded_json["first_name"];
panoMsg.last_name=decoded_json["last_name"];
panoMsg.msg_text=decoded_json["msg_text"];
panoMsg.put();
self.response.out.write(json.dumps({'StatusCode':'2', 'StatusMessage':'OK'}));
except ValueError:
logging.error("json-time sent data which simplejson couldn't parse")
self.response.out.write(json.dumps({'StatusCode':'4', 'StatusMessage':'NO'}));
def save_image_to_blobstore(base64str, mimeType):
from google.appengine.api import files
import binascii
fileName1 = files.blobstore.create(mime_type=mimeType)
with files.open(fileName1, 'a') as f:
f.write(binascii.a2b_base64(base64str))
files.finalize(fileName1)
return files.blobstore.get_blob_key(fileName1)
Upvotes: 0
Views: 803
Reputation: 3570
I haven't tested this but this should take in your base64 string and mime_type of the image to store it into the blobstore, then return the blob_key of that image:
def save_image_to_blobstore(base64str, mime_type):
from google.appengine.api import files
import binascii
file_name = files.blobstore.create(mime_type=mime_type)
with files.open(file_name, 'a') as f:
f.write(binascii.a2b_base64(base64str))
files.finalize(file_name)
return files.blobstore.get_blob_key(file_name)
You would use the returned blob key and store it in a Model that stores the other information in your JSON object as you described. Then you can serve up the image using a URL scheme you'd like (probably based on the key or ID or the Model that stores your other JSON info). See: https://developers.google.com/appengine/docs/python/blobstore/overview#Serving_a_Blob
Upvotes: 1