• http://www.techneurons.com/career/
  • experienced programming consultants for hire !!!

    Contact Now

    Google AppEngine Articles

    KB: Uploading a file to the AppEngine BlobStore (Python)

    THURSDAY, APRIL 7, 2011

    Google App Engine provides a Blobstore API that allows our app to serve 'Blobs' that are much larger in size say between 1 mb and 2 gb.Blobs are created by uploading a file through an HTTP request using a html form .When the form is submitted, the Google App Engine Blobstore creates a blob from the uploaded file returning the blob key that can be used to process the blob

    Form for uploading file

    from google.appengine.ext import blobstore
    class UploadFormHandler(webapp.RequestHandler):
        def get(self):
            upload_url = blobstore.create_upload_url('/upload')
            html = ''
            html += '<html><body>'
            html += '<form action="%s" method="POST" enctype="multipart/form-data">' % upload_url
            html += """Upload File: <input type="file" name="file"><br> <input type="submit"
                name="submit" value="Submit"> </form></body></html>"""
    
            self.response.out.write(html)

    The app generates the form's action URL(upload_url) by calling the Blobstore API. The user's browser uploads the file directly to the Blobstore via the generated URL.Blobs can't be modified after they're created, although they can be deleted. Each blob has a corresponding blob info record, stored in the datastore, that provides details about the blob, such as its creation time and content type.

    Processing the uploaded file

    class FileUploadHandler(blobstore_handlers.BlobstoreUploadHandler):
        def post(self):
            try:
                upload_files = self.get_uploads('file')
                logging.info('File upload received.  File count=%d' % len(upload_files))
                if len(upload_files) > 0:
                    blob_info = upload_files[0]
                    logging.info('Blob stored key=%s  % (blob_info.key()))
                  self.redirect('/new')
    
            except:
                logging.error('Error in Uploading')

    The above handler can do additional processing based on the blob key(blob_info.key()). Finally, the handler must return a headers-only, redirect response (301, 302, or 303), typically a browser redirect to another URL handler.

    Complete Source Code

    from google.appengine.ext import webapp
    from google.appengine.ext import blobstore
    from google.appengine.ext import db
    from google.appengine.ext.webapp import blobstore_handlers
    from google.appengine.ext.webapp.util import run_wsgi_app
    import logging
    
    
    class UploadFormHandler(webapp.RequestHandler):
     def get(self):
             upload_url = blobstore.create_upload_url('/upload')
             html = ''
             html += '<html><body>'
       html += "%s" % upload_url
             html += '<form action="%s" method="POST" enctype="multipart/form-data">' % upload_url
             html += """Upload File: <input type="file" name="file"><br> <input type="submit"
                name="submit" value="Submit"> </form></body></html>"""
             self.response.out.write(html)
    class FileUploadHandler(blobstore_handlers.BlobstoreUploadHandler):
     def post(self):
             try:
                  upload_files = self.get_uploads('file')
                  logging.info('File upload received %s.  File count=%d' % (upload_files[0].filename, len(upload_files)))
                  if len(upload_files) > 0:
                     blob_info = upload_files[0]
         logging.info('Blob stored key=%s ' % (blob_info.key()))
                     self.redirect('/new')
    
       except:
        logging.error('Error in prosessing the file')
        self.response.out.write('Error in prosessing the file')
    
    application = webapp.WSGIApplication([('/new',UploadFormHandler),
              ('/upload',FileUploadHandler)
             ],debug=True)
    def main():
     run_wsgi_app(application)
    if __name__ == '__main__':
     main()

     

    Reference : http://code.google.com/appengine/docs/python/blobstore/overview.html

    ConsultSarath - We provide end to end outsourcing solutions for .net programming requirements- you can hire programmer for hourly rates, for monthly commitments, for short term projects, for long term projects, Contact to know our hourly rates for programmer in India. ConsultSarath - We provide end to end outsourcing solutions for php programming requirements- you can hire programmer for hourly rates, for monthly commitments, for short term projects, for long term projects, Contact to know our hourly rates for programmer in India. ConsultSarath - We provide end to end outsourcing solutions for python programming requirements- you can hire programmer for hourly rates, for monthly commitments, for short term projects, for long term projects, Contact to know our hourly rates for programmer in India.

    Other Popular Articles
    We are experts in Cloud Computing Technologies. We can assist you to build high scalable business applications using Amazon Web Services (Amazon EC2, Amazon S3, Amazon SES, SNS, CloudFront), Windows Azure Platforms - Windows Azure and SQL Server Azure, Google App Engine using Python and Django Framework. We are Expert Programming Consultants available at affordable rates per hour. We work on several technologies - .NET, Python, Google App Engine, PHP, Windows Azure, Amazon Web Services ...