Search code examples
python-3.xgoogle-cloud-platformndb

Check Entity Size in Google cloud


I am trying to find the entity size in google cloud. However entity._to_pb() is throwing an attribute error. Can anyone help to find entity size from google cloud ndb.

    from google.cloud import ndb

    _MAX_ENTITY_SIZE_DATASTORE = 1048572

    def CheckEntitySizeTooBig(entity):
        max_size = _MAX_ENTITY_SIZE_DATASTORE
        if len(entity._to_pb().Encode()) > max_size:
            return False
        return True

AttributeError: 'ImageryRequest' object has no attribute '_to_pb'.

Is there any new way to find entity size which i am missing out. Please help.


Solution

  • Below code worked for me

    from google.cloud.ndb.model import _entity_to_protobuf
    
    _MAX_ENTITY_SIZE_DATASTORE = 1048572
    
    def CheckEntitySizeTooBig(entity):
        max_size = _MAX_ENTITY_SIZE_DATASTORE
        entity_to_proto = _entity_to_protobuf(entity)
        if len(entity_to_proto().SerializeToString()) > max_size:
            return True
        return False