Skip to content

Instantly share code, notes, and snippets.

@lucadealfaro
Created March 8, 2016 22:39
Show Gist options
  • Save lucadealfaro/61c39060e2ceefc31f6d to your computer and use it in GitHub Desktop.
Save lucadealfaro/61c39060e2ceefc31f6d to your computer and use it in GitHub Desktop.
from google.appengine.ext import blobstore
from google.appengine.ext import ndb
# Model for data.
class StoredFiles(ndb.Model):
file_id = ndb.StringProperty()
blob_key = ndb.BlobKeyProperty()
def get_upload_url():
"""Generates and returns an upload URL."""
# Assume I want to upload a file with id "id".
file_id = "id" # for example
u = URL('default', 'upload', args=[file_id])
uu = blobstore.create_upload_url(u)
return response.json(dict(upload_url=uu))
def upload():
"""Processes an upload to blobstore."""
# We need to get the blob key from the upload.
blob_info = blobstore.parse_blob_info(request.vars.content)
if blob_info is not None:
blob_key = blob_info.key()
# We have our blob_key, now we need to store it somewhere, likely together with some other
# parameters such as request.args(0) which corresponds to arg0 in controller/upload/arg0
c = StoredFiles()
c.file_id = request.args(0) or raise HTTP(500)
c.blob_key = blob_key
c.put()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment