본문 바로가기
파이썬

파이썬 Google App Engine에서 파일 업로드

by º기록 2020. 9. 25.
반응형

사용자가 Visual Studio 프로젝트 파일을 다운 그레이드 할 수있는 웹 앱을 만들 계획입니다. 그러나 Google App Engine은 db.TextProperty db.BlobProperty 를 통해 Google 서버에 파일 업로드 및 플랫 파일 저장을 허용하는 것으로 보입니다.

이 작업을 수행 할 수있는 방법에 대한 코드 샘플 (클라이언트 및 서버 측 모두)을 누구든지 제공 할 수있어 기쁩니다.

 

해결 방법

 

여기에 완전한 작업 파일이 있습니다. Google 사이트에서 원본을 가져 와서 좀 더 현실적으로 만들기 위해 수정했습니다.

주의해야 할 몇 가지 사항 :


이 줄의 목적은 ServeHandler class is to "fix" the key so that it gets rid of any name mangling that may have occurred in the browser (I didn't observe any in 크롬)

blob_key = str(urllib.unquote(blob_key))

이 끝에있는 "save_as"절이 중요합니다. 파일 이름이 브라우저로 전송 될 때 왜곡되지 않도록합니다. 무슨 일이 일어나는지 관찰하기 위해 그것을 제거하십시오.

self.send_blob(blobstore.BlobInfo.get(blob_key), save_as=True)

행운을 빕니다!

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>""")

        for b in blobstore.BlobInfo.all():


class UploadHandler(blobstore_handlers.BlobstoreUploadHandler): def post(self): upload_files = self.get_uploads('file') blob_info = upload_files[0] self.redirect('/') class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler): def get(self, blob_key): blob_key = str(urllib.unquote(blob_key)) if not blobstore.get(blob_key): self.error(404) else: self.send_blob(blobstore.BlobInfo.get(blob_key), save_as=True) def main(): application = webapp.WSGIApplication( [('/', MainHandler), ('/upload', UploadHandler), ('/serve/([^/]+)?', ServeHandler), ], debug=True) run_wsgi_app(application) if __name__ == '__main__': main()

 

참조 페이지 https://stackoverflow.com/questions/81451

 

 

반응형

댓글