I am using a custom python framework, (not django or webapp2), following this instructions: Create an upload URL, I have been available to upload the file, basically I submit my form to an URL provided by a script with this:
from google.appengine.ext import blobstore
upload_url = blobstore.create_upload_url('/upload')
By checking the blobstore viewer I notice that the file has been uploaded, this brings my first question.
How to filter, before uploading? for example, how to upload only if an image type is a jpg or png and discard anything else.
My second question is, how to properly handle the the uploaded file, what I found so far is to create a custom get_uploads method, this is what I am using but wondering if is the right way:
import cgi
from google.appengine.ext import blobstore
class APIResource(object):
def get_uploads(self, request, field_name=None):
if hasattr(request, '__uploads') == False:
request.environ['wsgi.input'].seek(0)
fields = cgi.FieldStorage(
request.environ['wsgi.input'],
environ=request.environ)
request.__uploads = {}
for key in fields.keys():
field = fields[key]
if isinstance(field, cgi.FieldStorage):
if 'blob-key' in field.type_options:
request.__uploads.setdefault(
key, []).append(
blobstore.parse_blob_info(field))
if field_name:
try:
return list(request.__uploads[field_name])
except KeyError:
return []
else:
results = []
for uploads in request.__uploads.itervalues():
results.extend(uploads)
return results
def dispatch(self, request, response):
blob_info = self.get_uploads(request, 'picture')[0]
print blob_info, blob_info.key(), request.__uploads
I would like to avoid uploading first, later check if type is valid and delete if not, so that I can not fill my storage with garbage in case I can't handle/get the upload key.
Any ideas ?