python 使用google appengine blobstore api上传的Flask示例。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 使用google appengine blobstore api上传的Flask示例。相关的知识,希望对你有一定的参考价值。

# coding: utf-8

from __future__ import absolute_import

from google.appengine.ext import ndb
from google.appengine.ext import blobstore
import cloudstorage as gcs

import flask
from flask.ext import restful

import json
import config
import model

from api import helpers
from main import api_v1
import logging


# NOTE: https://cloud.google.com/appengine/docs/python/blobstore/

"""The model
class Recording(model.Base):
  blob_key = ndb.BlobKeyProperty()

  FIELDS = {
    'blob_key': fields.BlobKey,
  }
"""


@api_v1.resource('/run/<string:run_key>/recording/', endpoint='api.recording')
class RecordingAPI(restful.Resource):
  def post(self, run_key):
    data = flask.request.get_json()
    recording_db = model.Recording()
    recording_db.put()
    blob_key = create_file(recording_db.key, data)
    recording_db.blob_key = ndb.BlobKey(blob_key)
    recording_db.put()
    return helpers.make_response(recording_db, model.Recording.FIELDS)


def create_file(key, data):
  # Create a GCS file with GCS client.
  filename = "/%s/%s" % (config.CONFIG_DB.bucket_name, key.urlsafe())
  with gcs.open(filename, 'w', content_type='application/json') as f:
    f.write(json.dumps(data))

  # Blobstore API requires extra /gs to distinguish against blobstore files.
  blobstore_filename = '/gs' + filename
  return blobstore.create_gs_key(blobstore_filename)

以上是关于python 使用google appengine blobstore api上传的Flask示例。的主要内容,如果未能解决你的问题,请参考以下文章

Google Appengine:Java 或 Python [重复]

如何使用Appengine和来自API的Python脚本流数据将数据流式传输到Google Cloud BigQuery?

为整个应用设置 Python27 Google AppEngine 默认编码

在没有 lxml 的 Google Appengine 上使用 python-amazon-product-api [重复]

来自 Python AppEngine 的 Google Url Shortener API:HTTPError:HTTP 错误 403:禁止

python 来自https://developers.google.com/appengine/docs/python/datastore/transactions