How do I calculate the size of an entity stored in Google App Engine?

前端 未结 2 358
遥遥无期
遥遥无期 2020-12-15 23:54

Is there an easy way to calculate the size of an entity stored in App Engine? I would like to know how close a particular entity is to hitting the 1 MB upper limit on entity

2条回答
  •  萌比男神i
    2020-12-16 00:47

    App engine stores each entity as a protobuf. You can use the db.model_to_protobuf() function described here to manually convert your entity into a protobuf and then use the standard len() method to determine its size in bytes.

    Example usage:

    from google.appengine.ext import db
    sz_in_bytes = len(db.model_to_protobuf(some_entity).Encode())
    

    Update for ndb

    Kekito points out in the comments below that for ndb entities a different approach is needed (Thanks Kekito!):

    len(some_entity._to_pb().Encode())
    

提交回复
热议问题