ファイルをアップロードGoogle App Engine
-
09-06-2019 - |
質問
私は計画を作るwebアプリケーション可能なユーザーへの格下げをvisual studioプロジェクトファイルです。しかし、そうでGoogle App Engineを受け入れファイルのアップロードやフラットファイルを格納できるGoogleのサーバーを通して db.TextProperty
や db.BlobProperty
.
んな嬉しいものを提供できるコードのサンプル(クライアントとサーバ側から行うことができます。
解決
ここでは、ファイルです。まったところを見計らい、元からGoogleのサイトの変更で少します。
いくつかの通知:
- このコードを使用し BlobStore API
ことを目的に行 ServeHandlerクラスは、"固定"の 鍵なく任意の名前 難号化する場合に発生した のブラウザ(Iは認められなかったは クローム)
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():
self.response.out.write('<li><a href="/serve/%s' % str(b.key()) + '">' + str(b.filename) + '</a>')
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()
他のヒント
実際、この問いに答えるアプリEgnineます。例を見る アップロードユーザー画像.
HTMLコード、 <form></form>:
<input type="file" name="img"/>
Pythonコード:
class Guestbook(webapp.RequestHandler): def post(self): greeting = Greeting() if users.get_current_user(): greeting.author = users.get_current_user() greeting.content = self.request.get("content") avatar = self.request.get("img") greeting.avatar = db.Blob(avatar) greeting.put() self.redirect('/')
Googleが公開されるサービスのための格納に大きなファイルです。してい blobstoreのAPIドキュメント.場合のファイル>1MBを使うことができます。
ようにしていない作品は、以下のことが明らかとなった。
私のアプリケーションを素早く開発では1.3倍となっております。x
htmlページ:
<form enctype="multipart/form-data" action="/upload" method="post" >
<input type="file" name="myfile" />
<input type="submit" />
</form>
サーバーコード:
file_contents = self.request.POST.get('myfile').file.read()
場合にも問題は、チェックをご利用のenctypeのフォームタグ
No:
<form encoding="multipart/form-data" action="/upload">
有:
<form enctype="multipart/form-data" action="/upload">
できない店舗のファイルとしてはありません伝統的なファイルシステム。できるだけ保存している自DataStore(分野として定義されて BlobProperty)
である前にリンク:
class MyModel(db.Model):
blob = db.BlobProperty()
obj = MyModel()
obj.blob = db.Blob( file_contents )
個人としての見解説するチュートリアル こちらの 有利用の場合は、Javaの実行時間GAE.何らかの理由で私は、やっぱりアップロードファイルを使用
<form action="/testservelet" method="get" enctype="multipart/form-data">
<div>
Myfile:<input type="file" name="file" size="50"/>
</div>
<div>
<input type="submit" value="Upload file">
</div>
</form>
俺HttpServletクラスの何らかの理由なのにenctype'属性。取り除いていただいても、しかし、このとできないアップロード任意のファイルです。
がフラットファイルの保存にGoogle App Engine.もよく、 Datastore するこのファミリー向けリレーショナルデータベースが必ずしもそうではありません。
お店のファイルとして TextProperty または BlobProperty 属性です。
あ1MB制限DataStore応募するものではありません。
にいるかの奇妙な行動時にファイル共有ソフトウェアアプリケーションエンジンです。ルが届かない場合は以下のお申し込みフォーム
<form method="post" action="/upload" enctype="multipart/form-data">
<input type="file" name="img" />
...
</form>
それぞれの抽出 img
からの要請にこのように:
img_contents = self.request.get('img')
の img_contents
変数は str()
Google Chromeでunicodeに達しています。そして、現在、 db.Blob()
コンストラクタを文字列という場合にエラーを通過するunicode文字列になります。
いうかこのとき固定ですか。
また、何を見たいのは絶対に不思議ながることをコピー&ペーストのゲ用(アバター)を動作させることができます。いものを正確に同じように私のコードが、それだけではない。私は非常に近く私の髪の毛ます。
あの使い方 フラットファイルシステム(転用の観点)
っているものもあり、これ Google App Engine仮想ファイルシステムのプロジェクト.実施するのdatastore、memcacheのApiをエミュレートする通常のファイルシステム.この図書館利用できますプロジェクト 類似のファイルシステムのアクセス(読み込みおよび書き込み).