LoginSignup
4
4

More than 5 years have passed since last update.

GAEのハマらないためのメモ

Last updated at Posted at 2015-02-28

Blobstore

Google Cloud EndpointsでblobstoreアップロードURLを生成してはならない

2015/02時点

  • Google Cloud Endpoints内でblobstore.create_upload_url()で取得したURLにファイルをアップロードすると、必ずtemporary-blobstore-error.appspot.comへリダイレクトされる。
  • console.developers.google.comのlogには何も残らない。
  • dev_appserverでは、問題ない。

これができないと、Google OAuth2.0認証とアップロードファイルを関連付けるのが、少々面倒になる。

  1. Google OAuth2.0認証に基づいたセッションを含めた、通常のApp EngineインスタンスにマッピングされるURLを発行する。
  2. 1.で発行したURLへファイルをPOSTさせる。
  3. 1.で発行したURLのRequest Handlerでは、セッションを含めたURLをblobstore.create_upload_url()へ渡してURLを取得し、このURLへstatus code 307でリダイレクトする。
  4. 3.で発行したURLからリダイレクトされたURLのRequest Handlerでは、Request URLに含まれるセッションからGoogle OAuth2.0認証のアカウントを判定する。

1.で発行するURLには署名が必要である。有効期限とソルトを加えた方がよい。
4.に相当するURLには署名は不要である。blobstoreの仕様上、3.で発行するURLはワンタイムであり、4.に相当するURLは事前に露出しないため。

マルチバイトなファイル名は文字化けしている

2015/02時点

  • 治ったフリされているが、ぜんぜん修正されていない。
  • dev_appserverでは、問題ない。
  • ファイル名がマルチバイトの場合、MIME encodeされた文字列が、そのままunicode型で格納されている。
  • POSTリクエストのcharsetによってMIME encodeのエンコーディングが変わる。base64 OR quopri
  • 以下のコードで、正しい文字列を取得できるようになる。
import email

for blob_info in self.get_uploads('file'):
  filename_mime = blob_info.filename
  if isinstance(filename_mime, unicode):
    filename_mime_utf8 = filename_mime.encode('utf-8')
  else:
    filename_mime_utf8 = filename_mime
  filename_encoded, encoding = email.header.decode_header(filename_mime_utf8)[0]
  if encoding is not None:
    filename_unicode = filename_encoded.decode(encoding)
    filename_utf8 = filename_unicode.encode('utf-8')
    blob_info._BlobInfo__entity['filename'] = filename_utf8

BlobInfo.creationのタイムゾーンはPST

2015/02時点

  • 2月に書いているので確認できないが、PDTかもしれない。
  • 信用ならない値なので、別途ndb.DateTimeProperty(auto_now_add=True)を保存するしかない。

Developers Console

このタイプの編集はサポートされていませんなpropertyを含むentityを編集すると、そのpropertyは壊れる

っぽい。

2015/02時点

このタイプの編集はサポートされていませんなproperty以外だけを編集しても壊れるっぽい。

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4