PYTHON-数据存储中 TextProperty 的 属性“{field_name}”的值超过 1048487 字节

PYTHON-The value of property "{field_name}" is longer than 1048487 bytes for TextProperty in datastore

根据字段类型 TextProperty 的云数据存储文档,不应该对大小有任何限制,但在我的例子中,字符串大小等于

时出现以下错误

The value of property "{my_field_name}" is longer than 1048487 bytes

下面是我的实体class

class History(ndb.Model):
    user_mail = ndb.StringProperty()
    json_string = ndb.TextProperty(indexed=False)
    updated_date = ndb.DateTimeProperty()

这是我尝试存储实体的函数

def store_user_mail(user_email, json_object):

    # string size for which this is creating problem is around 1.56Mb
    print(sys.getsizeof(json.dumps(json_object)))

    user_mail_obj = History()
    user_mail_obj.user_mail = user_email
    user_mail_obj.json_string = json.dumps(json_object)
    user_mail_obj.updated_date = datetime.datetime.utcnow()

    user_mail_obj.put()

我也曾尝试在存储之前压缩 json 字符串,但这会给我们带来问题,因为我们想将其存储为文本 属性 而不是二进制数据。

另外一点是因为我的 json_string 字段是 TextProperty 类型,无论字符串大小如何,它都应该毫无问题地存储。已经花了很多时间找出解决方案。我在这里做错了什么,或者是 NDBdatastore 的错误,如果是这样的话,什么是好的解决方案?

这不是 TextProperty 的问题,这是因为 Datastore 实体的最大大小可以为 1,048,572。 https://cloud.google.com/datastore/docs/concepts/limits

我正在为你查看 BlobProperty.... 看起来可能仍然是 1MB 的限制(我无法确认),但你可以使用 compress将超过 1MB 的数据放入其中的论点。

实体有 1MB 的大小限制。如果您尝试在 entity/property 中存储的内容可能大于该值,那么您需要使用 CloudStorage。我建议将该文本作为文件存储在 Cloud Storage 中,并在对象的 属性 中保留 link。它可能看起来像这样:

class History(ndb.Model):
    user_mail = ndb.StringProperty()
    url = ndb.StringProperty() #https://storage.googleapis.com/{app_id}/{file_name}
    updated_date = ndb.DateTimeProperty()

然后当您需要该文本时,您只需读取存储在 url 中的文件即可。