[英]App Engine: What is the best way to store >1MB of text?
我想在App Engine NDB中存储1MB以上的文本,但是NDB的实体大小限制为1MB。 我还能如何存储1 MB的unicode文本?
我做了很多。 最初,我将文本文件存储在Google Cloud Storage中,并将文件地址存储在我的实体中。 但是,我停止这样做了,因为我发现Google Cloud Storage的API过于不可靠。 我存储/检索文件的请求中有很大一部分会在重试时超时并继续超时(而且我的文件不是很大,大多数文件都在1MB以下)。
为了找到更好的解决方案,我改用了NDB压缩属性。 我的文本文件具有很好的可压缩性,因此压缩时我的几乎所有文件都应在1MB的限制之下。 为了简化操作,我创建了一个自定义属性(请参见下文)。
我的自定义属性取决于压缩的ndb.BlobProperty
,我只向UTF8添加了编码和解码。 在某些时候,我将进一步扩展它以在输入文本超过大小阈值时将文本存储在Google Cloud Storage中。
如果您希望压缩文本小于1MB,那么这对您来说将是一个很好的解决方案。 否则,您应该使用GCS Python客户端,但要注意超时错误。
class UTF8BlobProperty(ndb.BlobProperty):
"""
This is a custom blob property for storing unicode text as utf-8.
Later, we can add storing to GCS if text is too large.
"""
def __init__(self):
super(UTF8BlobProperty, self).__init__(default="", compressed=True)
def _validate(self, text):
if not isinstance(text, basestring):
raise TypeError("Expected a basestring, got %s" % text)
def _to_base_type(self, text):
return text.encode("utf-8")
def _from_base_type(self, text):
return text.decode("utf-8")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.