Skip to content

Instantly share code, notes, and snippets.

@johnboxall
Created September 2, 2010 23:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnboxall/563148 to your computer and use it in GitHub Desktop.
Save johnboxall/563148 to your computer and use it in GitHub Desktop.
from google.appengine.api import images
from google.appengine.api import urlfetch
from google.appengine.ext import webapp
from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers
PAYLOAD_TEMPLATE = \
"""--==boundary\r
Content-Disposition: form-data; name="file"; filename="%s"\r
Content-Type: %s\r
\r
%s\r
--==boundary--\r"""
class Post(webapp.RequestHandler):
upload = blobstore.create_upload_url('/upload')
headers = {'Content-Type': 'multipart/form-data; boundary="==boundary"'}
data = open("image.jpg", "r").read()
body = PAYLOAD_TEMPLATE % ('image.jpg', 'image/jpeg', data)
# !!! Hangs this request doesn't resolve on the dev_server or GAE.
response = urlfetch.fetch(upload, body, 'POST', headers, False, False, 10.0)
self.response.out.write('POSTed... %s' % response.final_url)
class Done(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
blob_info = self.get_uploads()[0]
next = images.get_serving_url(str(blob_info.key()))
self.redirect(next)
application = webapp.WSGIApplication([
('/upload', Done),
('/', Post),
], debug=True)
def main():
webapp.util.run_wsgi_app(application)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment