Upload the file to Google App Engine and make it downloadable

I am new to Google App Engine and I want to use it as a server so that users can upload a file. I have been trained in python. I did not find anything to actually be guided by how to upload files to the server for this purpose.

+4
source share
2 answers

The Blobstore tutorial gives an example only for this use case. This link provides this code: an application that allows users to upload files and then immediately upload them:

#!/usr/bin/env python # import os import urllib from google.appengine.ext import blobstore from google.appengine.ext import webapp from google.appengine.ext.webapp import blobstore_handlers from google.appengine.ext.webapp import template from google.appengine.ext.webapp.util import run_wsgi_app class MainHandler(webapp.RequestHandler): def get(self): upload_url = blobstore.create_upload_url('/upload') self.response.out.write('<html><body>') self.response.out.write('<form action="%s" method="POST" enctype="multipart/form-data">' % upload_url) self.response.out.write("""Upload File: <input type="file" name="file"><br> <input type="submit" name="submit" value="Submit"> </form></body></html>""") class UploadHandler(blobstore_handlers.BlobstoreUploadHandler): def post(self): upload_files = self.get_uploads('file') # 'file' is file upload field in the form blob_info = upload_files[0] self.redirect('/serve/%s' % blob_info.key()) class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler): def get(self, resource): resource = str(urllib.unquote(resource)) blob_info = blobstore.BlobInfo.get(resource) self.send_blob(blob_info) def main(): application = webapp.WSGIApplication( [('/', MainHandler), ('/upload', UploadHandler), ('/serve/([^/]+)?', ServeHandler), ], debug=True) run_wsgi_app(application) if __name__ == '__main__': main() 
+4
source

You can also check out the very good GAE / python application from Nick Johnson's blog, which has a nice interface and also allows multiple downloads if you need to. I used this code for my applications that need something like a file system, and block management allows this.

0
source

Source: https://habr.com/ru/post/1386223/


All Articles