Google App Engine blobstore错误:BlobNotFoundError

问题描述 投票:1回答:1

我使用Google App Engine blobstore来保存用户数据blob-大小从几百个字节到几百KB不等。 blob_info被另存为数据存储实体上的属性。

有时在生产中,从blobstore读取将失败,并显示BlobNotFoundError('')。 异常没有提供任何细节,我无法弄清楚为什么会发生故障。

根据Google的文档:

“如果blob没有引用实际的Blobstore值,则fetch_data会引发BlobNotFoundError。” https://developers.google.com/appengine/docs/python/blobstore/functions#fetch_data

“ fetch_data()函数找不到与给定BlobInfo或BlobKey值相对应的Blobstore值。” https://developers.google.com/appengine/docs/python/blobstore/exceptions#BlobNotFoundError

最令人困惑的是,故障是间歇性的。

以下是我用于读取/写入Blobstore的代码。 仅当blob_info(从数据存储读取)不是None时,才尝试读取。

有什么建议么?

def read(blob_info):
    blob_reader = blobstore.BlobReader(blob_info.key(), buffer_size=358400)
    try:
        data = blob_reader.read()
    finally:
        blob_reader.close()

    return data


def write(data, mime_type):
    file_name = files.blobstore.create(mime_type=mime_type)

    with files.open(file_name, 'a') as f:
        f.write(data)

    files.finalize(file_name)

    blob_key = files.blobstore.get_blob_key(file_name)

    # This is a hack to handle an apparent GAE delay synchronizing the blobstore
    for i in range(1,3):
        if blob_key:
            break
        else:
            time.sleep(0.05)
            blob_key = files.blobstore.get_blob_key(file_name)

    new_blob_info = blobstore.BlobInfo.get(str(blob_key))

    return new_blob_info
python google-app-engine blobstore
1个回答
0
投票

我认为您针对GAE延迟的破解运行了3次None成功,将blob_key的值设置为None 。 在行中

new_blob_info = blobstore.BlobInfo.get(str(blob_key))

str(blob_key)将转换为“ None”,即包含单词None的字符串。 因此,您将返回带有密钥的blob_info,其中仅包含单词“ None”。

您可以通过在循环后检查明确的None来快速修复它。

正确的解决方法是以某种方式避免出现种族状况,但是我不知道为什么会发生这种情况。 您的代码对我来说看起来很正常。 我在Java上做过大致相同的事情,而且看起来工作正常。 请记住,blobstore文件支持是实验性的,没有真正有据可查。 您可以重新编写代码以使用原始Blobstore API(即带有上传URL)吗?

© www.soinside.com 2019 - 2024. All rights reserved.