如何通过appengine端点api上传图像?

时间:2015-02-09 08:03:18

标签: google-app-engine google-cloud-datastore google-cloud-endpoints app-engine-ndb endpoints-proto-datastore

我正在使用Endpoints Proto Datastore API。 假设我有这样的代码:

型号:

class MyModel(EndpointsModel):
  _message_fields_schema = ('name', 'image')
  name = ndb.StringProperty()
  image = ndb.BlobProperty()

API:

@endpoints.api(name='myapi', version='v1', description='my api')
class MyApi(remote.Service):

@MyModel.method(name='mymodel.insert', path='mymodel')
def insert_mymodel(self, data):
    data.put()
    return data

如何通过api将图像上传到数据存储区? 提前谢谢。

1 个答案:

答案 0 :(得分:1)

我自己没有做过,taken from here。您需要使用字节类型并将图像编码为base64:

  

使用Cloud Endpoints时,字节数   发送到BytesField必须是base64编码。

     

通过Google的API基础架构发送和验证后,   base64编码的字节将被发送到你的   protorpc.remote.Service类并从base64字符串转换为   Python中的native byte-string(str的实例)。

     

因此,您需要您的客户端获取图像字节并进行转换   到base64。

     

要在Javascript中将字节字符串编码为base64,请参阅“如何使用”   使用Javascript?编码到Base64,简单地在Python中执行相同操作   调用

     

导入base64   base64.b64encode(byte_string)