如何通过 appengine 端点 api 上传图片?
How to upload an image through appengine endpoints api?
-
google-app-engine
-
app-engine-ndb
-
google-cloud-endpoints
-
endpoints-proto-datastore
-
google-cloud-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 将图像上传到数据存储区?
提前谢谢你。
我自己没做过但是 taken from here。您需要使用字节类型并将图像编码为base64:
When using Cloud Endpoints, the bytes
sent to a BytesField must be base64 encoded.
After being sent and validated through Google's API infrastructure,
the base64 encoded bytes will be sent along to your
protorpc.remote.Service class and converted from a base64 string to a
native byte-string (instance of str) in Python.
So you'll need your clients to take the image bytes and convert them
to base64.
To encode a byte string as base64 in Javascript, see How can you
encode to Base64 using Javascript?, to do the same in Python, simply
call
import base64
base64.b64encode(byte_string)
google-app-engine
app-engine-ndb
google-cloud-endpoints
endpoints-proto-datastore
google-cloud-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 将图像上传到数据存储区? 提前谢谢你。
我自己没做过但是 taken from here。您需要使用字节类型并将图像编码为base64:
When using Cloud Endpoints, the bytes sent to a BytesField must be base64 encoded.
After being sent and validated through Google's API infrastructure, the base64 encoded bytes will be sent along to your protorpc.remote.Service class and converted from a base64 string to a native byte-string (instance of str) in Python.
So you'll need your clients to take the image bytes and convert them to base64.
To encode a byte string as base64 in Javascript, see How can you encode to Base64 using Javascript?, to do the same in Python, simply call
import base64 base64.b64encode(byte_string)