【问题标题】:How to upload an image through appengine endpoints api?如何通过 appengine 端点 api 上传图片?
【发布时间】:2015-04-08 22:12:02
【问题描述】:

我正在使用 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 将图片上传到数据存储区? 提前谢谢你。

【问题讨论】:

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


    【解决方案1】:

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

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

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

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

    要在 Javascript 中将字节字符串编码为 base64,请参阅您如何 使用 Javascript 编码到 Base64?,在 Python 中做同样的事情,简单 打电话

    导入base64 base64.b64encode(byte_string)

    【讨论】:

    • 非常感谢。如果你能指出一些例子,这将非常有帮助。
    • 我想通了。非常感谢。
    猜你喜欢
    • 2022-07-26
    • 1970-01-01
    • 1970-01-01
    • 2021-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-21
    相关资源
    最近更新 更多